Install
Download and install the Go distribution for your platform from the official website (https://golang.org/). This will install the Go compiler and tools on your system.
Set the GOPATH environment variable. This variable tells Go where to find your workspace, which is a directory where you will store your Go code. On Windows, you can set the GOPATH variable by going to Control Panel > System and Security > System > Advanced System Settings > Environment Variables, and then adding a new user or system variable. On macOS or Linux, you can set the GOPATH variable by adding a line to your .bashrc or .bash_profile file, like this: export GOPATH=$HOME/go.
(Optional) Add the Go bin directory to your PATH. This will allow you to run Go commands from any directory. On Windows, you can do this by adding ;%GOPATH%\bin to the end of the PATH variable. On macOS or Linux, you can do this by adding :$GOPATH/bin to the end of the PATH variable.
Test your installation by opening a terminal or command prompt and running the go command. If Go is installed correctly, you should see a list of available subcommands.
Create a workspace directory and set up your Go project. To do this, create a new directory for your project, and then create a src subdirectory inside it. This will be the root of your Go workspace. You can then create a new Go package by creating a new directory inside the src directory, and adding a main.go file with some Go code.
Setting up a new Go project
- create a new directory for your project
mkdir newproj - change into that directory.
cd newproj - initialize the module with
go mod initThis will create a go.mod file in your project directory, which defines the dependencies of your project. - create a new file with a go extention (main.go by convention)
- write your code
for example:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
