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.goResult:
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 simpleQuotes are required. Without them — an error:
fmt.Println(Hello) // won't workMultiple 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 lineSecond lineThird lineEach 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 threeAll 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)}423.14-10You can combine text and numbers:
fmt.Println("Answer:", 42)Answer: 42Simple Arithmetic
package main
import "fmt"
func main() { fmt.Println(2 + 2) fmt.Println(10 - 3) fmt.Println(6 * 7) fmt.Println(15 / 4)}4742315 / 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 printCodeComments 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: helloText always needs quotes: "hello".
Fancy quotes
fmt.Println(«Hello») // wrongfmt.Println("Hello") // rightOnly straight double quotes ".
Typo in Println
fmt.Prinln("Hello") // missing tfmt.PrintLn("Hello") // capital LCase 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 1Line 2Line 3Solution
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 oldIn 10 years I'll be Y years oldWhere 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.