👋 Go "Hello world!" guide
Tags: go, beginners
Published: 2024-04-05 16:54:06

😎 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](https://go.dev/doc/install)

### Windows

Download and run the MSI installer from [go.dev/dl](https://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](https://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](https://go.dev/dl/).

```sh
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
```

<sup>Check [go.dev/dl](https://go.dev/dl/) for other non-x86-64 Linux builds</sup>

## 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.**

    ```go
    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](https://go.dev/tour/welcome/1)

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](https://go.dev/learn/)**

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lbnd013nxvrbivb0ztx.png)](https://go.dev/learn/)

---

{% embed https://github.com/jcbhmr/go-hello-world %}