Go Installation & Setup 🎉

beginner
21 min

Go Installation & Setup 🎉

Welcome to the exciting world of Go programming! In this comprehensive guide, we'll walk you through the installation and setup process, making sure you're well-prepared to dive into Go programming. By the end of this lesson, you'll have Go installed on your machine and be ready to start writing your first Go program. 🎯

What is Go?

Go, also known as Golang, is a modern, open-source programming language developed by Google. It's known for its simplicity, robustness, and strong support for concurrent programming. Go is widely used in a variety of projects, from web applications to system tools, making it a versatile and valuable skill to learn. 💡

System Requirements 📝

Before we begin, let's ensure your system meets the necessary requirements for Go installation:

  1. Operating System: Go supports most modern operating systems, including Linux, macOS, and Windows.
  2. Hardware: A 64-bit system is required for Go installation.
  3. Memory: Go requires at least 2GB of RAM to run comfortably.

Installing Go 🔧

Now that we've confirmed your system's compatibility, let's install Go! Follow the steps below according to your operating system:

On Linux and macOS

First, you need to download the Go installer. Open your terminal and run:

bash
curl -O https://golang.org/dl/go-linux-amd64.tar.gz

Next, extract the installer:

bash
tar -xvf go-linux-amd64.tar.gz

Now, move the Go binary to a system directory:

bash
sudo mv go /usr/local

Lastly, update your system's PATH variable to include the Go binary:

bash
echo 'export GOROOT="/usr/local/go"' >> ~/.bash_profile echo 'export GOPATH="$HOME/go"' >> ~/.bash_profile echo 'export PATH="$GOPATH/bin:$GOROOT/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile

On Windows

For Windows users, download the Go installer from the official Go download page. Run the installer, and follow the on-screen instructions to complete the installation.

Verifying Go Installation ✅

To confirm that Go has been installed correctly, open a new terminal/command prompt and run:

bash
go version

If the installation was successful, you should see the Go version displayed on the screen.

Setting Up Your Go Workspace 💼

Every Go project should have a designated workspace. By default, Go uses $GOPATH/src as the workspace. Let's create a src folder in your home directory:

bash
mkdir -p $HOME/go/src

Now you're all set to start writing Go code! Let's dive into some examples. 📝

Practical Examples 👨‍💻

Example 1: Hello, World!

Here's a simple "Hello, World!" program in Go:

go
package main import "fmt" func main() { fmt.Println("Hello, World!") }

Save this code in a file named main.go within your Go workspace (e.g., $HOME/go/src/hello). To compile and run the program, navigate to your workspace and run:

bash
go build ./hello

You should see "Hello, World!" printed on the screen. 🎉

Example 2: Basic Variables and Data Types 📝

Go has several basic data types, including:

  • int: Integer (32-bit)
  • float64: Double-precision floating-point number
  • string: String (array of bytes)

Here's a simple example demonstrating these data types:

go
package main import "fmt" func main() { var i int = 42 var f float64 = 3.14 s := "Go programming" fmt.Println(i, f, s) }

Save this code in a file named variables.go within your Go workspace, then compile and run it like before. ✅

Quiz 🔍

Quick Quiz
Question 1 of 1

What does Go use as the default workspace for Go projects?

Wrapping Up 🎓

In this lesson, you learned how to install Go, set up your Go workspace, and wrote your first Go program. You're now well-equipped to start exploring more advanced topics in Go programming.

Stay tuned for more lessons on CodeYourCraft, where we'll dive deeper into Go, teaching you the essential skills you need to become a proficient Go developer. Happy coding! 🚀