Go Basics

Chapter 3

Govet and Gofmt#

Govet is a tool that automatically runs and check constructs, helping to find out problems not caught by the compiler.

Code is auto-indented and formatting issues are taken care by Gofmt. Code style is enforced by default in the language which maintains consistency among all developers.

Unused Variables/Packages#

Go will not allow you to import packages that are unused or declared variables which are not used. This emphasizes and forces developers to write clean code.

Lexical Scope vs Function Scope#

Lexical scope is within the brackets surrounding it. Function scope lasts for the duration of the function execution.

Importing Packages#

To import packages, we can use the factored import statement.

package main
import (

Alternatively, we can also use multiple import statements.

import "fmt"
import "math"

We can indicate an alias as well.

import (
f "fmt" // f is the alias for "fmt" package

We can import a package and ignore it as well:

import (
_ "mypkg"

We can do this to benefit from the init() function inside the package. The init() function can help to initialize some drivers etc. which you may require.

Programs start running from the main package.