Inside the module folder, create a plain-text file named main.go and enter this program:
Go code
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}Line-by-line explanation
package mainsays this file belongs to a runnable program.import "fmt"brings in Go’s formatting toolbox.funcbegins a function definition.main()is the special starting function for a runnable Go program.- The opening brace begins the function body.
fmt.Printlndisplays one line of text.- The closing brace ends the function body.
Expected output
Hello, world!This is the program’s output after it runs.
Save the file, then run the current module:
Command
go run .Line-by-line explanation
gostarts the toolchain.runcompiles and immediately runs the program..means the current directory.
Expected output
Hello, world!Go compiles the source in a temporary location, runs it, and displays the message.