Getting started
Anatomy of a Go program
Let's revisit the previous program line by line.
package main import "fmt" func main() { fmt.Println("Hello, Go!")}`package main` declares the package this file belongs to. The `main` package is special: it's the one that produces an executable (any other package name produces a library). `import "fmt"` imports the standard formatted I/O package, which provides `Println`, `Printf`, `Sprintf`, and so on. `func main()` is the entry point: in a `main` package, this function is called automatically on startup.
Go refuses to compile a file that imports an unused package, or declares a local variable that is never read. This isn't a warning: it's a compile error. This strictness prevents dead code from piling up.
A real Go project is organized around a module, declared in a `go.mod` file at the root. It defines the module's import path and the Go version used, and references external dependencies.
go mod init example.com/hellomodule example.com/hello go 1.23Your turn
Without running any code: does the following file compile? Justify your answer. package main import ( "fmt" "os" ) func main() { fmt.Println("Hello") }
// Réfléchis avant de révéler la solution.