DEV Community

Cover image for πŸ‘‹ Go "Hello world!" guide
Jacob Hummer
Jacob Hummer

Posted on • Updated on

πŸ‘‹ Go "Hello world!" guide

😎 Welcome to the world of Go! Go (aka Golang) is a statically typed, compiled programming language designed to stay simple. Developed by Google, Go offers features like fast compile times, static type enforcement, garbage collection, concurrency support, and clean syntax, making it an excellent choice for building scalable and reliable software.

Now, let's get started with setting up your Go environment. πŸš€

Installing Go

πŸ“š Check the official go.dev/doc/install instructions for more details

Windows

Download and run the MSI installer from go.dev/dl and follow the onscreen prompts.

macOS

Download & open either the Apple macOS (ARM64) (new M-series Macs) or Apple macOS (x86-64) (older Intel Macs) package from go.dev/dl and follow the onscreen prompts.

Linux

⚠️ Make sure you update the go1.22.2 version specifier to the latest version listed on go.dev/dl.

wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go*.tar.gz
echo 'export PATH="$PATH:/usr/local/go/bin:$HOME/go/bin"' >> ~/.bashrc
# Then restart your shell to reload ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Check go.dev/dl for other non-x86-64 Linux builds

Creating your first Go program

  1. Create a new folder somewhere. This will be where we put the main.go, go.mod, etc. files. You might also want to open this folder in an IDE or the terminal.

  2. Run go mod init github.com/<your_username>/go-hello-world. This will create a go.mod file in the current directory. If you've used Rust or JavaScript before you can think of go.mod sorta like Cargo.toml or package.json. The github.com/<your_username>/go-hello-world specifier is the Go module specifier. It's what your friend might use to go install github.com/<your_username>/go-hello-world. This URL-like specifier should match where the code is accessible. Usually that's in a GitHub repository.

  3. Create the main.go file and add code to it.

    package main // "main" is a special package name
    import "fmt" // view docs on https://pkg.go.dev/fmt
    func main() { // the actual entry point
      fmt.Println("Hello world!")
    }
    

    🚌 Take a tour of the Go language on go.dev/tour

  4. Run go run . to run the code! The . refers to the current directory. You can also use go run to run other sub-packages that also have a package main in them. For instance you could mv main.go myapp/main.go and then run go run ./myapp.

  5. (Optional) Push your code to GitHub! Then you can use go install github.com/<your_username>/go-hello-world to compile & install your executable on any computer with the Go toolchain installed without the tedium of git clone and go run. πŸš€

Congratulations! πŸŽ‰ You've just created and run your first Go program!

πŸ“š Check out go.dev/learn for other learning resources

Image description


GitHub logo jcbhmr / go-hello-world

πŸ‘‹ An example "Hello world" app in Go

Go "Hello world!" example app

πŸ‘‹ An example "Hello world" app in Go
πŸ“„ Example for πŸ‘‹ Go "Hello world!" guide - DEV Community

package main

import "fmt"

func main() {
    fmt.Println("Hello world!")
}
Enter fullscreen mode Exit fullscreen mode
$ go-hello-world
Hello world!

Installation

go install github.com/jcbhmr/go-hello-world@latest
Enter fullscreen mode Exit fullscreen mode

Usage

go-hello-world
Enter fullscreen mode Exit fullscreen mode

Development

go run .
Enter fullscreen mode Exit fullscreen mode



Top comments (0)