π 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>/go-hello-world
. This will create ago.mod
file in the current directory. If you've used Rust or JavaScript before you can think ofgo.mod
sorta likeCargo.toml
orpackage.json
. Thegithub.com/<your_username>/go-hello-world
specifier is the Go module specifier. It's what your friend might use togo 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.-
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!") }
Run
go run .
to run the code! The.
refers to the current directory. You can also usego run
to run other sub-packages that also have apackage main
in them. For instance you couldmv main.go myapp/main.go
and then rungo 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 ofgit clone
andgo 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!")
}
|
|
Installation
go install github.com/jcbhmr/go-hello-world@latest
Usage
go-hello-world
Development
go run .
Top comments (0)