Variable Basics (Go)

Hello World in Go

Variables and programming languages go hand in hand, and Go is no different. In Go variables can be defined using the keyword var, the variable name and the variable type for example:

var name_of_player string

var player_score int

Or multiple variables can be defined using var () for example:

var (

       name_of_player string

       player_score int

)

geektechstuff_Go_variables_1
Go Variables at package level

Variable names must start with a letter or an underscore _

Uninitialised variables (i.e variables with no values store in them) are give the value of zero.

Go variables at Function level
Go variables at Function level

Variables defined at the function level are not global, so thought should be given to where a variable is created (package or function) and where it will be altered or called on.

Note: Any defined variables must be used otherwise the Go compiler will generate an error when it tries to compile the Go program.

There are a few types that variables can be:

  • string

A string is made up of characters e.g. alphabetical characters.

  • float

A float is a decimal point number e.g. 45.89 or -2999.38

  • int

An integer is a whole number e.g. 9 or -9

  • bool

A boolean value is TRUE or FALSE

Examples of Go variables
Examples of Go variables

To print the values of a variable:

fmt.Printf(“some text %v”, variable_name)

To print the type of a variable:

fmt.Printf(“some text %T”, variable_name)

Go example of printing variable value and variable type
Go example of printing variable value and variable type

As an alternative to declaring and then initialising them, variables can be initialised and declared at the same time using:

variable_name := variable_value

Passing Variable Values Between Packages / Functions

When passing variable values, Go actually copies the value to another memory address and passes the copied value. For example, if my variable of “name” contained “geektechstuff”, was set at the package level and this lived in memory address 0xAA when this variable is passed to function the value would stay the same but would be copied to another memory address (e.g. 0XBB) which would then be given to the function.

Looking at a variable's memory reference
Looking at a variable’s memory reference

A variable’s memory address pointer can be checked or used by referencing the variable with an ampersand (&) in front of it. To dereference a variable’s memory pointer| replace the ampersand (&) with an asterisk (*).

Constants

Unlike variables constants, as the same suggest, are constant which means they cannot be changed once set. Constants cannot be declared using the := method. To create a constant the const keyword is used e.g.

const name string = “geektechstuff”

would create a constant called name, that is a string type and contains the string geektechstuff.

Note: Any defined constants must be used otherwise the Go compiler will generate an error when it tries to compile the Go program.

 

Posted in Go

One thought on “Variable Basics (Go)

Comments are closed.