In this post I will write how to create your first Go program. The famous Hello World, that I will replace world for Go.
To get started, you’ll need to install Go on your system. Go to https://go.dev/doc/install and follow the instructions for your operating system
Once installed, verify your installation by opening a terminal and running
go version
This should display the installed Go version.
Now, let’s write our first Go program. Create a file named hello.go with the following content:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Save this file and run this program using the command below in your terminal:
$ go run hello.go
You will see in your terminal the output: “Hello, Go!” Now, let’s break down the program to understand what it is happening:
-
package main
: Every Go file starts with a package declaration. Themain
package is special because it defines an executable program. -
import fmt
: This importsfmt
package, which provides formatting and printing functions. -
func main()
: Themain
function is the entry point of the program. -
fmt.Println()
: This function prints a line to the console.
Congratulations! You’ve written and run your first Go program.
What will you build next? #Golang