## Go - Hello World

Most (if not all) programming language courses start with the classical 'hello world' example, these lessons are no exception. Your first Go program simply prints "Hello World" to the screen. Although the example on the Go official website prints 'Hello, 世界!', with 世界 meaning 'world'. Thus the example is slightly different to demonstrate that Go supports Unicode by default.

Our 'Hello World' example starts with package main, it simply means that the current file hello.go is in the main package. That is, the package that implements the main function, which is the function that is first executed when your program starts running.

The compiler simply looks for the function whose name is main and starts from there. And the main package is the first place to look.

The next line is import "fmt". As you may have guessed it already, it says : "Please, import fmt!", it is pretty straightforward.

fmt is a package name, like "main" in the first line. But this time, the line asks the compiler to import all the functionality that resides in the "fmt" package. Fmt is a package that is provided by the standard library (click here to see the package list), which comes with your Go installation.

So, as soon as you successfully install Go, you should be able to import and use the fmt package. For the record, fmt stands for "Format", and it implements formatted I/O. So, we use this package to print the output to the screen, receive inputs...etc.

Next, comes the line func main(){, again this is very intuitive, and it is a declaration of a function whose name is main with the keyword func, the parentheses () meaning that the function does not accept any arguments (we will see what this means in a future lesson about functions).

Then, the line fmt.Println("Hello World !") simply means : Print "Hello World!" to the screen, then append a newline (note the ln). So, Println is a function in the fmt package that prints a message to the screen, then, newline. Perfect!

The  fmt. before Println says : Call the function Println that is in package fmt. We will see this in more detail in future lessons.

Do not forget to use double quotes " " before and after Hello World, otherwise the message will get mixed up with code, use double quotes for text values (i.e strings).

Finally,  The curly bracket { after func main() denotes the start of our function's body, but how can our compiler know where our function ends? You are right! you have to close the curly brackets with }

That ends our lesson. Here is our code :

package main

import "fmt"

func main(){

fmt.Println("Hello World!")

}

Remember! Go is case sensitive, for example Println must start with capital P.

To test your first program, simply save it to hello.go in your goprojects directory, and execute this command :

\$ go run hello.go

You can use go build hello.go instead to build an executable binary file that will be run directly, but that would be useful for larger, and more interesting, applications.

As a final note, since the newline marks the end of an instruction and the start of a new one in Go (i.e: the famous semicolon ";" is not necessary, although you can use it), you should always insert a newline before you type the next instruction.

That's it for this lesson, in the next lesson we learn about variables, constants, declarations...

### Quiz

• Write a small program that prints "Go is awesome!" [Answer]

• Is this line correct? fmt.println("Hello World!")? justify? [Answer]

• Is this line correct? fmt.Println("Hello!");? justify? [Answer]

• Is this code correct? Func main(){? justify? [Answer]

• What does this line mean: import "./mypackage"? [Answer]

• Is this line correct: fmt.Println('Go!')? [Answer]

Incorrect. Messages are marked using double quotes " ", and not single quotes ' '.