👋 Go “Hello world!” guide

👋 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

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

Creating your first Go program

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.

Run go mod init github.com/<your_username>/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>/hello-world specifier is the Go module specifier. It’s what your friend might use to go install github.com/<your_username>/hello-world. This URL-like specifier should match where the code is accessible. Usually that’s in a GitHub repository.

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

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.

(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


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!”)
}

$ go-hello-world
Hello world!

Installation

go install github.com/jcbhmr/go-hello-world@latest

Usage

go-hello-world

Development

go run .

Leave a Reply

Your email address will not be published. Required fields are marked *