Your first Go program

Contents
package main
func main
go run
go build

package main

Create a new folder named hello and save the following text as main.go inside it.

The first line declares package main, which tells the compiler that this code builds an executable program and not a library.

package main import "fmt" func main() { fmt.Println("Hello, Go course") }

func main

Execution always starts in the main function, which takes no arguments and returns nothing.

The body calls Println from the fmt package to print one line of text, and the opening brace stays on the same line as required by Go.

go run

The go run command compiles the sources into a temporary binary, starts it at once, and removes it after the run.

It is perfect for quick experiments while learning.

go run main.go

Hello, Go course

go build

The go build command produces a permanent binary file that can be started any number of times without the compiler.

go build -o hello main.go

./hello

Hello, Go course

Use go run during development and go build when the program is ready to share or deploy.

Next: Modules with go mod

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: