Your first program
Context
Section titled “Context”Every Go program is composed of packages. A package named main is special: it defines an executable program. The entry point is a function named main with no parameters and no return values.
Structure of a Go source file
Section titled “Structure of a Go source file”package main // Package declaration
import "fmt" // Import statement (standard library)
func main() { // Entry point function fmt.Println("Hello, World") // Statement}Explanation
Section titled “Explanation”package main– Declares that this file belongs to themainpackage.import "fmt"– Imports thefmtpackage for formatted I/O.func main()– The program starts here. No arguments, no returns.fmt.Println– Prints a line to standard output.
Running the program
Section titled “Running the program”Save the code as main.go, then in a terminal:
go run main.goExample
Section titled “Example”Create a file main.go with the content below.
Code example
Section titled “Code example”package main
import "fmt"
func main() { fmt.Println("Hello, World")}Output
Section titled “Output”Hello, World