HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›Introduction to Go

Introduction to Go

First step into Go, a fast and simple language: the structure of every program (package/import/main) and printing with fmt.Println.

Simple enough for a 5-year-old to understand

The structure of a Go program

Every Go program starts with package main, imports packages with import, and runs from the main() function.

Your first program
1
package main
Package for an executable program.
DefinitionPackage for an executable program.
2
import "fmt"
Import the fmt package for output.
ImportImport the fmt package for output.
3
func main() {
The program starts here.
FunctionThe program starts here.
4
fmt.Println("Hello!")
Prints to the screen.
Output / InputPrints to the screen.
5
}
End of main.
FunctionEnd of main.
DefinitionImportFunctionOutput / Input
💡 Hint: These four lines (package/import/func main) form the skeleton of every Go program.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.Which function does every Go program start running from?
2.Print exactly Hello World to the screen.

🎯 Expected output: Hello World

Loading editor…

About this topic

Go was designed to solve maintenance problems of large software systems at Google. Its design philosophy is clear: the fewer features a language has, the easier the code is to read. That is why Go lacked inheritance, exception mechanisms, and generics for a long time.

Go is a compiled language and produces a single executable file — you don't need to install dependencies, you just copy the file and run it. Compilation time is very short, which is Go's most praised practical feature.

Go's strictest rule is formatting: there is a tool called gofmt and it dictates how code is written. Indentation, spacing, and bracket placement are not up for debate. Also, UNUSED variables and imports are a compilation ERROR — not a warning, but an error.

What you will learn

  • the package and func main structure
  • printing output with the fmt package
  • import declarations
  • the rule for unused variables and imports
  • the difference between go run and go build

When to use?

fmt.Println is the primary tool for debugging. For formatted output, fmt.Printf is used, and its format specifiers come from C; the percentage v specifier, unique to Go, prints any value in its default format and is the most commonly used.

In production code, the log package or structured logging is preferred; Println writes to standard output and has no level filtering.

Common mistakes

Wrong

package main

import "fmt"

func main() {
	x := 5
}

An unused variable is a compilation ERROR in Go: "declared and not used: x". Also, because fmt is imported and not used, you get an "imported and not used" error as well. Go allows zero flexibility on this.

Correct

package main

import "fmt"

func main() {
	x := 5
	fmt.Println(x)
}

Wrong

package main

import "fmt"

func main()
{
	fmt.Println("Hello")
}

In Go, the opening curly brace MUST be on the SAME line as the function signature; writing it on the next line gives a "syntax error: unexpected semicolon or newline before {" error. This rule is a technical necessity because Go automatically inserts semicolons at the end of lines.

Correct

package main

import "fmt"

func main() {
	fmt.Println("Hello")
}

Wrong

package main

import "fmt"

func Main() {
	fmt.Println("Hello")
}

The name of the entry point function must be main in lowercase, and the package must also be main. When written with an uppercase letter, the compiler gives a "function main is undeclared in the main package" error.

Correct

package main

import "fmt"

func main() {
	fmt.Println("Hello")
}

Frequently asked questions

Why is an unused variable an error instead of a warning?

Go's design choice: as warnings accumulate, they get ignored and the code becomes cluttered. It forces the language to stay clean by throwing an error. You can temporarily silence it by assigning the value to an underscore, but permanent use of this is not recommended.

What is the difference between go run and go build?

go run compiles and runs immediately without leaving behind an executable file — practical during development. go build, on the other hand, writes the file to disk and is used for deployment. The generated file is standalone; you only need to copy that file to the server.

What do names starting with an uppercase letter mean?

Go does not have access modifiers; the first letter of the name determines it. Anything starting with an uppercase letter is accessible from outside the package, while lowercase stays inside the package. This is a simple and consistent rule replacing the public and private keywords.

Summary

Go is a language with few features and fast compilation that produces a single standalone executable file. An unused variable and import is a compilation error, formatting belongs to gofmt, and access is determined by the first letter of the name.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code