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. 🎯
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. 💡
Before we begin, let's ensure your system meets the necessary requirements for Go installation:
Now that we've confirmed your system's compatibility, let's install Go! Follow the steps below according to your operating system:
First, you need to download the Go installer. Open your terminal and run:
curl -O https://golang.org/dl/go-linux-amd64.tar.gzNext, extract the installer:
tar -xvf go-linux-amd64.tar.gzNow, move the Go binary to a system directory:
sudo mv go /usr/localLastly, update your system's PATH variable to include the Go binary:
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_profileFor 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.
To confirm that Go has been installed correctly, open a new terminal/command prompt and run:
go versionIf the installation was successful, you should see the Go version displayed on the screen.
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:
mkdir -p $HOME/go/srcNow you're all set to start writing Go code! Let's dive into some examples. 📝
Here's a simple "Hello, World!" program in 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:
go build
./helloYou should see "Hello, World!" printed on the screen. 🎉
Go has several basic data types, including:
int: Integer (32-bit)float64: Double-precision floating-point numberstring: String (array of bytes)Here's a simple example demonstrating these data types:
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. ✅
What does Go use as the default workspace for Go projects?
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! 🚀