Hello World
In the Go tutorial, you get started with Go in the typical manner: printing“Hello World” (Ken Thompson and Dennis Ritchie started this when they presentedthe C language in the 1970s). That’s a great way to start, so here it is, “HelloWorld” in Go.
package main 1
import "fmt" 2 // Implements formatted I/O.
/* Print something */ 3
func main() { 4
fmt.Printf("Hello, world.") 5
}
Lets look at the program line by line. This first line is just required 1. AllGo files start with package <something>
, and package main
is required fora standalone executable.
import "fmt"
says we need fmt
in addition to main
2. A package otherthan main
is commonly called a library, a familiar concept in many programminglanguages (see ). The line ends with a comment that begins with //
.
Next we another comment, but this one is enclosed in /
/
3. When your Goprogram is executed, the first function called will be main.main()
, whichmimics the behavior from C. Here we declare that function 4.
Finally we call a function from the package fmt
to print a string to thescreen. The string is enclosed with "
and may contain non-ASCII characters5.