पाठ 2 / 26
Hello, World and the go Command
go run, go build, go mod init.
One tool for everything
The go command builds, runs, tests and manages dependencies: go mod init example.com/app creates a module, go run . compiles and runs the package in the current directory, go build produces a binary, go test ./... runs tests, go vet finds suspicious code and gofmt formats it. A program starts in package main at func main(). Imports must be used, and unused variables are compile errors, which keeps code clean.
A first program, run
I ran this with Go 1.27.1 (go run . in a module named demo, standard library only). fmt.Printf formats with verbs such as %s for strings and %d for integers.
package main
import "fmt"
func main() {
name := "Gopher"
fmt.Printf("Hello, %s! You have %d new messages.\n", name, 3)
}
Output:
Hello, Gopher! You have 3 new messages.
Use go run while learning
go run . compiles and runs in one step; use go build when you need the binary.
त्वरित जाँच: Where does a Go program start running?
- func main() in package main
- The first function in any file
- init.go
- A class with a static main method
Answer
func main() in package main — package main + func main.