Go Programming/Getting Started

From Wikibooks, open books for an open world
Jump to: navigation, search

Hello World[edit]

Let's start with a simple example, the venerable hello world program.

package main

import "fmt"

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

As you can see there is a lot going on here. The first line:

package main

starts every go source file. It states which package this file belongs to. Think of a package as a bag of code, where you are going to stuff the internals of your programs into. The main package is special, and will we get into that in a moment.

import "fmt"

The line declares all the packages (or bags of code others have written to help us do things — also known as libraries) we are going to use. In this case, we are going to use the fmt (the format package: pronounced fa-umpt) Again, in later sections, we will delve deeper into packages.

func main(){

}

Here we declare a function (```func```). A function is a set of instructions that we provide to the computer to get it to do what we want it to do. There are few special functions in go,