DEV Community

Cover image for ๐Ÿฆซ๐Ÿš€ Golang Guide
Joรฃo Victor
Joรฃo Victor

Posted on

๐Ÿฆซ๐Ÿš€ Golang Guide

Welcome to our guide on getting started with Go (Golang)! ๐ŸŒŸ In this article, we will explore how this powerful programming language, developed by Google, can help you build efficient and high-performance applications. Weโ€™ll cover the essentials of installing Go, setting up your development environment, and creating your first project, as well as how to optimize your builds for different platforms.

Whether youโ€™re new to programming or an experienced developer, this guide will set you on the path to mastering Go. For more insights and to explore my other repositories or access this post in Portuguese, be sure to visit my GitHub profile at my GitHub.

๐Ÿ” What is Go (Golang)?

Go, also known as Golang, is an open-source programming language developed by Google. It was created in 2009 with the goal of providing efficiency, simplicity, and high performance for building distributed systems, web applications, and network services.

๐Ÿ›  Installing Go

To start programming in Go, follow these steps to install the compiler and set up the development environment:

๐Ÿ“ฅ Download and Install Go

Visit the official Go website and download the appropriate version for your operating system. Follow the installation instructions provided.

๐Ÿ”ง Setting Up the PATH

After installation, it's important to configure the PATH so that your system can find the Go binaries. Add the Go bin directory to your PATH to execute Go commands from the terminal or command prompt.

โœ… Verifying the Installation

Open your terminal or command prompt and type:

go version
Enter fullscreen mode Exit fullscreen mode

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

๐Ÿ— Starting a Project in Go

The basic structure of a Go project involves packages. Each package is stored in a directory with the same name as the package. Here's how to start a simple project.

๐Ÿ“‚ Basic Project Structure

  1. Create a project directory: For example, create a folder named my-project.
  2. Create a main file: Inside that directory, create a file named main.go. This file is the default entry point for Go programs.

To initialize a Go project, run the following command:

go mod init my-project
Enter fullscreen mode Exit fullscreen mode

This command will create a go.mod file that will manage your project's dependencies by keeping track of all imported packages.

๐Ÿ’ป Example Go Code (Our Famous "Hello World")

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

This code simply prints "Hello, World!" when executed.

Running Your Code

  1. Open your terminal or command prompt.
  2. Navigate to the directory where your main.go file is located.
  3. Run the command:
go run main.go
Enter fullscreen mode Exit fullscreen mode

This command will compile and run the program.

๐Ÿ—๏ธ Building Your Go Project

To compile your Go project into an executable file, you can use the following command:

go build
Enter fullscreen mode Exit fullscreen mode

This command automatically detects the current operating system and architecture.

If you want to specify the operating system and architecture during the build, use this command:

GOOS=linux GOARCH=amd64 go build main.go
Enter fullscreen mode Exit fullscreen mode

In this example, we are building the project for a Linux system with a 64-bit architecture, even if you're on a Windows machine.

๐ŸŒ Environment Variables for Cross-Compilation

  • GOOS: This variable sets the target operating system for the build.

    • Common values:
    • linux: For Linux systems
    • windows: For Windows systems
    • darwin: For macOS systems
  • GOARCH: This variable sets the target processor architecture.

    • Common values:
    • amd64: For 64-bit systems
    • 386: For 32-bit systems
    • arm: For ARM devices
    • arm64: For 64-bit ARM devices

Example Cross-Platform Build Commands

  • For Linux:
  GOOS=linux GOARCH=amd64 go build my_game.go
Enter fullscreen mode Exit fullscreen mode
  • For Windows:
  GOOS=windows GOARCH=amd64 go build -o my_game.exe my_game.go
Enter fullscreen mode Exit fullscreen mode
  • For macOS:
  GOOS=darwin GOARCH=amd64 go build my_game.go
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Build Optimization Flags

  • Reduce the size of the build:
    • Using the -ldflags "-s -w" flag removes unnecessary information from your program.
  go build -ldflags "-s -w" my_game.go
Enter fullscreen mode Exit fullscreen mode
  • Find concurrency issues:
    • The -race flag helps detect issues when different parts of your program try to access shared data simultaneously.
  go build -race my_game.go
Enter fullscreen mode Exit fullscreen mode

๐Ÿค– Automating the Build Process with a Script

You can create a magic script that builds versions of your game for multiple computers all at once. Here's an example script:

#!/bin/bash

PLATFORMS=("windows/amd64" "linux/amd64" "darwin/amd64")
APP_NAME="my_game"

for PLATFORM in "${PLATFORMS[@]}"
do
    PLATFORM_SPLIT=(${PLATFORM//\// })
    GOOS=${PLATFORM_SPLIT[0]}
    GOARCH=${PLATFORM_SPLIT[1]}
    OUTPUT_NAME=$APP_NAME'-'$GOOS'-'$GOARCH
    if [ $GOOS = "windows" ]; then
        OUTPUT_NAME+='.exe'
    fi  

    echo "Building for $GOOS/$GOARCH"
    env GOOS=$GOOS GOARCH=$GOARCH go build -o $OUTPUT_NAME my_game.go
done
Enter fullscreen mode Exit fullscreen mode

โ“ What to Do If Something Goes Wrong?

  • Compatibility Error: Make sure you're using the correct GOOS and GOARCH values for the target system.
  • Permission Issues: If your program doesn't run, you might need to give it execution permissions:
  chmod +x my_game
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“š Documentation and Resources

For more information and in-depth guides, visit the official Golang documentation.

๐ŸŽ‰ Conclusion

Now you know how to set up, build, and optimize your Go programs so that they work on any computer! ๐Ÿš€ Keep learning and experimenting, and you'll become a Go programming master in no time! ๐Ÿ˜„

Top comments (1)

Collapse
 
pecksbadboy profile image
pecks-bad-boy

Don't forget about M series Macs:
GOOS=darwin GOARCH=arm64 go build my_game.go