This tutorial aims to guide you through installing Go, also known as Golang, and setting up your development environment. By the end of this tutorial, you will have a fully operational Go development environment on your computer.
First, download the Go binary release suitable for your system from the official download page: https://golang.org/dl/.
After downloading, install it. The process will vary depending on your operating system:
Windows: Open the MSI file and follow the prompts to install Go. By default, Go will be installed in C:\Go
, and its bin directory, C:\Go\bin
, will be added to your PATH.
macOS: Open the package file and follow the prompts to install Go. The package installs the Go distribution to /usr/local/go
.
Linux: Extract the downloaded archive and install to /usr/local
:
bash
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Add /usr/local/go/bin
to the PATH environment variable:
export PATH=$PATH:/usr/local/go/bin
You can test the installation by opening a new terminal window and running:
go version
This should display the installed version of Go.
After installing Go, create a workspace where you'll store your Go code. By convention, this is usually named go
. Make a directory named go
in your home directory:
mkdir ~/go
Next, set the GOPATH
environment variable to point to your workspace:
export GOPATH=$HOME/go
Then, add your workspace's bin
subdirectory to your PATH
:
export PATH=$PATH:$GOPATH/bin
Let's create a simple Go program:
Inside your workspace, create a new directory src/hello
:
bash
mkdir -p $GOPATH/src/hello
Inside this directory, create a file named hello.go
and add the following code:
```go
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
```
package main
defines the package name of your program.import "fmt"
imports the fmt
package which contains functions for formatting text, including printing to the console.func main()
is the main function where your program starts.You can run this program with go run
:
bash
go run hello.go
This should output: Hello, world!
You have now installed Go and set up your Go development environment. You've also written and run a simple Go program. Next steps could include exploring more of the Go standard library and learning about Go's type system and concurrency features.
Modify the hello.go
program to output Hello, YOURNAME
, replacing YOURNAME
with your actual name.
Write a program that adds two numbers and outputs the result.
Write a program that outputs the current date and time.
Remember, practice is key when learning a new programming language. Keep experimenting with new concepts and techniques.