Skip to main content

Introduction: Hello World, values, and variables

Go code layouts

A Go project is also called a module. A module is just a collection of packages.

A package is just a group of related .go files. You would declare the .go files that belong in the same package with the line

package <package name>

For example: If you use the main package it is used to make the package an executable program (you get a binary) because it contains your main function. The main package tells the Go compiler that the package will be compiled as an executable program rather than a library which will not produce an executable.

Otherwise, the package name can be whatever you want. However, keep the package name that you are declaring the same as the directory that it is under. For example:

src
	helper
    	- helper.go
    main.go

If you have a directory like such keep the package name that you use in helper.go as helper because if do package lol which doesn't match the directory name. You would be importing the helper package in main.go as

import (
	"module/path/helper
)

But when you want to call the function from the lol package it would be

lol.helperFunc()

So keeping the directory name and the package name the same would make it easier for yourself and for others to maintain.

Hello World

package main

import "fmt"

func main() {

    var a = "initial"
    fmt.Println(a)

    var b, c int = 1, 2
    fmt.Println(b, c)

    var d = true
    fmt.Println(d)

    var e int
    fmt.Println(e)

    f := "apple"
    fmt.Println(f)
}

As you can see the fmt module that is imported is the built-in module in Golang for printing things out to the consoles.

Println is just one of the functions inside fmt module to print things, there are many others.

Notice that the function is capitalized. This is not arbitary but rather telling Go that the Println function from the fmt module is exported for others to use. If it is lower-case then it cannot be used by others.

Variables and Values