Previous Lecture Complete and continue  

  Your First Go Program

Tip:

Below the transcript of this lecture, you can find

  1. A small quiz to challenge yourself
  2. The source code from this lecture for download

In general, lectures may offer additional information after the video transcript, so ensure to scroll down to end of the transcript in each lecture.

Transcript

So many courses and tutorials start with a “hello world” application. This course makes no difference. After all, a “hello world” application can already reveal quite some details about a programming language. So here we go.

The package declaration

The first line of a Go program is a package declaration.

package main

Go programs can be composed of packages. The package named “main” always gets compiled into an executable binary. All other packages are libraries.

Importing a package

To use a library, import the package by its path. Go comes with a large standard library that has packages for a vast variety of tasks available. Let’s import the “format” library.

import (
    "fmt"
)

The format library, sometimes also pronounced “fmt”, provides functions for handling formatted input and output of text.

We’ll see an example in a few moments. First, let’s create a function definition.

The main function

func main() {
}

The func keyword initiates the function definition. main is the name of the function. The parentheses contain function parameters. In this particular case, the function does not take any parameters, so the parens are empty. Likewise, the function does not return anything; otherwise the return type would have to be declared after the parens.

This, by the way, is similar to many other programming languages.

A function called “main” is always the main entry point of an executable. There can only be one function of that name, and it must reside in the package named “main”.

Ok, now let’s have the main function do something. Remember that we have imported the format package, so we now can call a function from that package. For this, just type the package name, a dot, and the function name.

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

And that’s it! We can run this code directly from the source file, using the go run command.

$ go run main.go

Go run takes one or more file names, and compiles and runs the code on the fly. It does not create a binary file; this makes the tool useful for quick tests that shall not leave anything behind.

Comments

One last thing to mention here are comments. You can add comments to your code using C++- or Java-style comments. That is,

  • Two slashes start a line comment. This comment ends where the line ends.
  • A slash and an asterisk start a multi-line comment. This comment type is ended by an asterisk followed by a slash.

As with C++ and Java, comments cannot be nested.

To summarize

  • Every Go program starts with a package declaration.
  • Name your package “main” if you want to create an executable.
  • All other package names create libraries.
  • Use the import keyword to add a library to your code.
  • Call a library function as “package name” dot “function name”.
  • The function named “main” is the entry point of your program.
  • Compile and execute a single Go file using go run.
  • Two slashes start a line comment. A multiline comment starts with a slash and an asterisk and ends with an asterisk and a slash.

Download
Discussion
4 comments