This is continuation to my year old Go Introductions, I realized it's time to write some tutorials to Dream In Code again and as I have been looking at Go again I decided to continue writing tutorials about it.

Go Variables

The Go syntax is somewhat different from languages like C++, Java or C#. In this tutorial we will look at how to declare and utilize variables and constants in Go.

Identifiers

The Go identifiers begin with a letter in Unicode UTF-8 or and underscore followed by zero or more letters or Unicode digits.

LETTER = UNICODE_LETTER or "_"
IDENTIFIER = LETTER followed by 0 or more { LETTER or UNICODE_DIGIT }

Few examples of valid identifiers:

Sucker

index0

foo

f00

_bar

__foobar

The important difference with Go is that Go uses Unicode and not just ASCII letters. This means you can even use greek letters in your identifiers. The Go designers have taken step towards making Go a more internationalized language.

In contrast, the following are not valid identifiers:

0bar

case

+2d

The first one is illegal because it starts with digit and not letter or "_". The second is reserved keyword so it can not be used as identifier. The third is illegal because it contains operator.

The underscore by itself is special identifier known as the blank identifier. This can be used in a declaration or value assignment like any other identifier, but the value is discarded. This is quite useful with Go functions and methods that return multiple values, because sometimes you do not need all of the return values returned by the functions or methods.

Constants
Constants in go can be one of three types:

Numbers

Strings

Booleans

You generally declare constant this way:

const Identifier = value;

const is a keyword indicating that the following is a constant. Identifier is the name of the constant and value is the value of the constant.

Constants must be able to be evaluated at compile time. Calculations are acceptable, as long as they involve values that are able to be evaluated at compile time and do not require any runtime activites.

Note that type specifier is optional.

Variables
The general form of declaring variable is:

var Identifier DataType = value;

var is keyword indicating that the following statement will be a variable. Identifier is the name of the variable. DataType is the type of the data the variable will contain and Value is the initial value of the variable.

Variables in Go are similar to constants, except they can be initialized with values computed at runtime. For example, lets say We wanted to know what the environment variable GOOS is set to. If you recall during the installation GOOS is the environment variable that indicates the platform we installed Go on.

We can use the following code to determine this particular environment variables contents:

package main; // We create main package
import (
"fmt"
"os"
) // We used the short import to import fmt and os packages.
func main() { // remember { must be on the same line as func main() or the compiler will error.
var goos string = os.Getenv("GOOS"); // we declare goos variable and assign the goos environment variable in runtime to it.
fmt.Printf("The operating system is: %s\n", goos); // we print the goos variable to terminal
}

The above code is a complete program and as you can see go allows you to obtain environment variables with the os.Getenv() function which takes a single, string parameter and returns a string indicating the contents of the environment variable you specify.

Also important to note is that, like constants, the data type identifier is optional, as long as there is an initialization expression. One or the other, or both are acceptable.

For example:

var a string;
a = "hello";
var b = "hell"; // both a and b are acceptable
var c; // c is not acceptable

While Go is forgiving with its unique typing system, it is still technically strongly typed. Just declaring var t; does not give Go any idea as to what the type of data will be.

Time savers: Short declarations and distributing

There is couple handy tools in Go's arsenal to make declarations and value assignment statements even shorter.

The first of these are short declarations. Short declarations allow you to skip having to type the keywords, or even the data type of the variable or constant.

For example:

var myInt int = 16;

Can be shortened to:

myInt := 16;

When short declaration is used, the data type of the identifier is determined by the value. Notice the := is used for a short declaration, while our old friend the = is used in traditional declarations.

To shorten even further, it is also acceptable to forget the usage of semicolon ( off at the end of statements. Go automatically inserts semicolon before compilation if necessary. Personally I prefer to use semicolon at the end of such statements and expressions.

Another handy technique is distributing of a keyword. We've seen this already with our import statements:

import (
"fmt"
"os"
)

The parentheses allow you to distribute the keyword. The above is equivalent to:

import "fmt";
import "os";

We can use distribution with variables and constants too. It can become quite cumbersome to have to keep writing the same keyword over and over again, because of this Go allows us to use this technique to save time when coding, increasing the writability of the language.

var (
foo int = 12;
bar string = "foo";
cowcount float = 12.5;

Notice that mixing the datatypes doesn't matter. All the distribution does is just what it's name implies. it distributes the keyword across the identifiers. Notice that also the same rules for declaring all of the seperately apply when using distribution. The above is equivalent to:

var foo int = 12;
var bar string = "foo";
var cowcount float = 12.5;

This ends the tutorial for Go variables. Next time we will see some basic data types such as integers, floats, strings, characters, etecra. Wait for it or remind me to write it by PM.