A Programmer's Guide To Go With Visual Studio Code
Written by Mike James   
Monday, 08 July 2019
Article Index
A Programmer's Guide To Go With Visual Studio Code
Variables & Structs
Types
Parameters
Go Control

Variables And Simple Data Types

Go has all of the primitive data types you might expect from uint8 to float64.

  • uint8  unsigned 8-bit integers (0 to 255)
  • uint16 unsigned 16-bit integers (0 to 65535)
  • uint32 unsigned 32-bit integers (0 to 4294967295)
  • uint64 unsigned 64-bit integers (0 to 18446744073709551615)

  • int8  signed 8-bit integers (-128 to 127)
  • int16 signed 16-bit integers (-32768 to 32767)
  • int32 signed 32-bit integers (-2147483648 to 2147483647)
  • int64 signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

  • float32  IEEE-754 32-bit floating-point numbers
  • float64  IEEE-754 64-bit floating-point numbers

  • complex64  complex numbers with float32 real and imaginary parts
  • complex128  complex numbers with float64 real and imaginary parts

  • byte alias for uint8
  • rune alias for int32

The big surprise is that it supports a complex numeric type:

var z complex64
z = 1.0 + 2.0i
fmt.Println(z)

And if you are wondering what a rune is then the mystery is solved once you know that t rune is used to store a Unicode code point. In other words, rune is Go's equivalent of a character type. 

You can also make use of uint, int and uintptr which are machine dependent integers with either 32 or 64 bits. 

Another surprise is that when you declare a variable you put the type at the end not the beginning, i.e var a int and not int a as you would in C or Java. 

You don't have to specify a type if you make an assignment within the declaration and if you don't assign an initial value the variable is set to its zero value.  This means you can write things like:

var i=0
var x,y float32=1.0,2.0

As well as numeric types you also have a Boolean type bool.

These are Go's built in types.

A short-cut to  initializing and declaring variables is provided using

x,y:=1,2

You can also define and use constants. 

 

goruns

Data Structures

When it comes to data structures you have the usual strings, arrays and structs and one welcome extra the map. 

Strings work with Unicode and are immutable but otherwise much as what you would expect:

s="Hello"

You can find the length of string using the len function and you can access individual bytes of a string using indexing s[0].  Strings in Go are fairly primitive but with the addition of the string package you can do everything that you can do in other languages. 

Arrays are declared using square brackets and start from zero index. For example:

var buff [32]byte
fmt.Println(buff[10])

Mutlidimensional arrays are build up as arrays of arrays as in many other languages:

var buff [32][32]byte
fmt.Println(buff[10][0])

Arrays are not dynamic and cannot be resized. However you can use a slice to get the same effect. A slice wraps a portion of an array and can change its size by changing the area of the array it gives you access to. 

Stucts are constructed much like in other languages. For example:

func main() {
    type point struct {
      x, y int
    }

    var p = point{10, 10}
    fmt.Println(p.x)
}

This declares a new struct type with two int fields x, and y. In the main function an new instance of the type is created and initialized. Go doesn't use the term "instance" but prefers the term "value" and you create a value of the type. 

A struct definition can also include a struct as a field. The initializer {10,10} is  struct literal. You can also use struct literals with named fields e.g {X:10}.

This is our first encounter with the idea of a Go type - more on this important topic later.

The final data structure available for your use is the Map. This is the Go equivalent of the hash map, associative array or dictionary in other languages. 

You can set up a map with a given type of key and a given type of value. If you have never used an associative array then think of it like an array where you can retrieve the value using a general key rather than just an index. For example:

var m = make( map[string]int) 
m["mike"] = 10
m["lucy"] = 30
fmt.Println(m["lucy"])

displays the result 30. 

The make function is one of the two functions which create values from types - and to make any sense of it we next have to find out more about types. 



Last Updated ( Monday, 08 July 2019 )