Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Hello World

Go is installed — time to write some code.

Create a file called main.go and paste this in:

package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

Run it:

Окно терминала
go run main.go

Result:

Hello, World!

If you see that line, everything’s working. What each line of code means — we’ll cover in the next lesson. For now, let’s just have a play.


Changing the Text

Replace "Hello, World!" with anything you like:

fmt.Println("Go is simple")
Go is simple

Quotes are required. Without them — an error:

fmt.Println(Hello) // won't work

Multiple Lines

Add more calls to fmt.Println:

package main
import "fmt"
func main() {
fmt.Println("First line")
fmt.Println("Second line")
fmt.Println("Third line")
}
First line
Second line
Third line

Each Println outputs text and moves to a new line.


Output Without a Newline

There’s Print — without ln at the end:

package main
import "fmt"
func main() {
fmt.Print("One ")
fmt.Print("two ")
fmt.Print("three")
}
One two three

All on one line.


Printing Numbers

Numbers work without quotes:

package main
import "fmt"
func main() {
fmt.Println(42)
fmt.Println(3.14)
fmt.Println(-10)
}
42
3.14
-10

You can combine text and numbers:

fmt.Println("Answer:", 42)
Answer: 42

Simple Arithmetic

package main
import "fmt"
func main() {
fmt.Println(2 + 2)
fmt.Println(10 - 3)
fmt.Println(6 * 7)
fmt.Println(15 / 4)
}
4
7
42
3

15 / 4 gives 3, not 3.75 — integers divide to a whole number. There’ll be a separate lesson on that.


Comments

Text after // is ignored:

package main
import "fmt"
func main() {
// this is a comment, the programme doesn't see it
fmt.Println("But this will print")
fmt.Println("Code") // a comment at the end of a line works too
}
But this will print
Code

Comments are for leaving notes to yourself or others. Or to temporarily disable code:

// fmt.Println("This line won't run")
fmt.Println("But this one will")

Common Mistakes

Forgot the quotes

fmt.Println(hello)
undefined: hello

Text always needs quotes: "hello".

Fancy quotes

fmt.Println(«Hello») // wrong
fmt.Println("Hello") // right

Only straight double quotes ".

Typo in Println

fmt.Prinln("Hello") // missing t
fmt.PrintLn("Hello") // capital L

Case matters: Println, not PrintLn or Prinln.


Exercises

1. Your Greeting

Print your name:

Hello, my name is [your name]!
Solution
package main
import "fmt"
func main() {
fmt.Println("Hello, my name is Alex!")
}

2. Multiple Lines

Print:

Line 1
Line 2
Line 3
Solution
package main
import "fmt"
func main() {
fmt.Println("Line 1")
fmt.Println("Line 2")
fmt.Println("Line 3")
}

3. All on One Line

Print Go is brilliant using three separate Print calls:

Solution
package main
import "fmt"
func main() {
fmt.Print("Go ")
fmt.Print("is ")
fmt.Print("brilliant")
}

4. Calculator

Print the result: 123 + 456 = ???

The number after = should be calculated by the programme.

Solution
package main
import "fmt"
func main() {
fmt.Print("123 + 456 = ")
fmt.Println(123 + 456)
}

Or on one line:

fmt.Println("123 + 456 =", 123+456)

5. Age

Print:

I'm X years old
In 10 years I'll be Y years old

Where X is your age, Y is calculated by the programme.

Solution
package main
import "fmt"
func main() {
fmt.Println("I'm 25 years old")
fmt.Println("In 10 years I'll be", 25+10, "years old")
}

What’s Next

You’ve run your first programme and experimented with output. But what do package main, import "fmt", func main() mean? We’ll cover that in the next lesson.


Sources