Go variables, types, constants, and imports - Golang

Go Gopher Programming Language Declaring a variable in Go

Use the var keyword followed by the identifier followed by the type.

var varName type

var num int

Declaring and initializing a variable in Go

var varName type = value

var name string = ‘Joseph’

Shorthand for declaring and initializing a variable in Go

Go figures our what type the variable is even though it does not specify a type.

varName := value

name := ‘Joseph’

Declaring multiple variables at once

var var1, var2, var3 type

var num1, num2, num3 int

Declaring multiple variables at once and initializing values

var  var1, var2, var3 type = value1, value2, value 3

Shorthand for declaring multiple variables at once and initializing values

You can use the colon equals operator to declare and initialize the variable.

var1, var2, var3 := val1, val2, val3

Grouping declaration and initialization

Go will figure out the type if one is not specified.

var (
   a int = 0           // integer
   b     = 1.618 + 6i  // complex number
   c     = 3.1415  // floating-point number
)

Declaring constants

Use the const keyword before the identifier. Once a constant is declared, its value cannot be changed.

const num1 float64 = 3.14157

Built-in types with Go

Boolean

Booleans are used to represent the truth of something, so they can hold a value of true or false. Also, the zero value of a boolean type is false.

var y bool            // y has a zero value of false

var x bool = true // x has a value of true

Numeric Types

  • Integers - int and uint will have a bit size of the computer running Go.
    • Signed
      • int, int8, int16, int32, int64
    • Unsigned
      • uint, uint8, uint32, uint64
  • Floating-point numbers
    • float32, float64
  • Complex numbers
    • complex64, complex128

Strings

A string is a sequence of letters or characters.

var name string = “Joseph”  // name = “Joseph”

The zero value of the string type is an empty string (“”)

var color string // color = “”

Strings follow zero-based numbering and are represented in UTF-8 format.

Character J o s e p h

Index     0 1 2 3 4 5

Iota constant generator

Go's iota identifier is used in const declarations to simplify definitions of incrementing numbers. Because it can be used in expressions, it provides a generality beyond that of simple enumerations.

The values of iota is reset to 0 whenever the reserved word const appears in the source (i.e. each const block) and increments by one after each ConstSpec e.g. each Line. This can be combined with the constant shorthand (leaving out everything after the constant name) to very concisely define related constants.

Import Statements

The keyword “import” is used for importing a package into other packages.

import (
   “fmt”
)

The package “fmt” comes from the Go standard library. When you import packages, the Go compiler will look on the locations specified by the environment variable GOROOT and GOPATH. Packages from the standard library are available in the GOROOT location. The packages that are created by yourself, and third-party packages which you have imported, are available in the GOPATH location.

Import statements can be grouped

import (
   "fmt"
   "os"
   "github.com/aeiou/json"
)

Installing third-party packages

Download and install third-party Go packages by using “Go get” command. The Go get command will fetch the packages from the source repository and put the packages on the GOPATH location.

go get github.com/aeiou/json

 

Visit sunny St. George, Utah, USA