Golang Variables – Types and Naming Conventions

Golang Variables Types and Naming Convention

Variables play a very important role in any language. In This Blog, we will learn about Golang Variables.

Let’s Learn!

Golang Hello World

package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}

Golang Variable Types

There are three ways in which we declare Golang variables.

1. Golang Variables Declaration

var <variable_name> <datatype>

var i int

i = 17

or we can combine both of the lines into one.

2. Golang Variables Declare and initialize

var <variable_name> <datatype> = <value>

var i int = 17

3. Golang Variables Short declaration

<variable_name> := <value>

i := 17

These are Golang variable declarations and initialization. It looks a bit ugly when compared with other languages syntax like, in C++ variable declaration follows

<datatype> <variable_name> = <value>; => int i = 20;

Let’s look at the difference between these three Go variables declaration.

package main

import (
	"fmt"
)

var i int = 27 // Package level variable
func main() {
	var i int = 17 // Block level Variable
	var j int
	j = 81
	k := 17. // Go compiler automatically detects it's datatype as float64
	fmt.Println("Hello World")
	fmt.Printf("%v,%T\n", i, i)
	fmt.Printf("%v,%T\n", j, j)
	fmt.Printf("%v,%T", k, k)
}

Output of the code is:

Hello World
17,int
81,int
17,float64

Golang declare global variable

package main

import (
	"fmt"
)

var i int = 27 // Golang Global Variable
func main() {
	fmt.Printf("%v,%T\n", i, i)
}

Output:

27, int

We cannot declare and initialize variables using the third method (the shorthand method) in the package level of the program.

Golang Variables Block

Every time writing ‘var‘ during variable declaration and initialization is tiring. There is a way of declaring all the Go variables inside a block.

var (
	a int = 9
	b string = "abc"
	c float64 = 99.0
	d bool = true
)

In this way, some of our work in variable declaration and initialization in Go is reduced.

Golang Variable Scope

Function level variables take precedence and this is called Shadowing.

package main

import (
	"fmt"
)

var i int = 27 // Package level variable

func main() {
	var i int = 17 // Block level Variable
	var j int
	j = 81
	k := 17. // Go compiler automatically detects it's datatype as float64
	fmt.Println("Hello World")
	fmt.Printf("%v,%T\n", i, i)
	fmt.Printf("%v,%T\n", j, j)
	fmt.Printf("%v,%T", k, k)
}

The Output of ‘i’ in this case is 17 but there is a global declaration of a variable with the same name ‘i’, this is shadowing. Function level variables have precedence over the global variables.

Golang Variables Naming Convention

There are mainly three conventions regarding naming of Go variables .

  1. Start with a lower case letter for scope only to the package. Any file in the same package can access the variable.
  2. Start with an upper case letter, if you want it to be exported from the package and globally visible.
  3. Acronyms in UPPERCASE. (HTTP, URL .. etc)

Golang variables Type Conversion

var i int = 12
var j int
j = float32(i)
fmt.Printf("%v,%T\n", j, j)

Output:- 12, float32

var i float32 = 12.80
var j int
j = i

Error – cannot use i (type float32) as type int in assignment

There is no implicit type conversion of Go variables / Go Programming Language, the programmer has to explicitly convert the data type of variables.

Golang variables integer to string

var j int = 81
var k string = string(j)
fmt.Printf("%v,%T\n", k, k)

Output:- Q, string

The integer value is converted to the alphabet using the ASCII value. But we wanted the number as a string. In this case we will have to import Golang strconv Package in order to use the Itoa function, which converts the number to a string.

import (
	"fmt"
	"strconv"
)
var j int = 81
var k string = strconv.Itoa(j)
fmt.Printf("%v,%T\n", k, k)

Output:- 81, string

Also read Future of Server side language – Golang.

Golang Documentation

Download Golang from the link and read the documentation to get started with Golang.

Golang Types of variable declaration

There are three types of variable declaration in Golang or the Go Programming Language.
1. Declare and initialize
=> var i int = 123
2. Declare and initialize it later
=> var i int
=> i = 123
3. Shorthand Method of declaring and initializing variables in GO
=> i := 123

Golang Variable declaration Conventions

1. You can’t redeclare the same variable but can shadow its value.
2. All the variables declared must be used, this keeps our code clean.
3. The length of the variable name should depend on its lifespan.
4. Pascal or camelCase Naming Convention

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *