A Programmer's Guide To Go With Visual Studio Code |
Written by Mike James | ||||||
Monday, 08 July 2019 | ||||||
Page 2 of 5
Variables And Simple Data TypesGo has all of the primitive data types you might expect from uint8 to float64.
The big surprise is that it supports a complex numeric type:
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 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:
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
You can also define and use constants.
Data StructuresWhen 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:
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:
Mutlidimensional arrays are build up as arrays of arrays as in many other languages:
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: 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:
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 ) |