DEV Community

Cover image for Variables, types and Keywords in go

Posted on

Variables, types and Keywords in go

In the blog, I want to look into variables, basic types, keywords and control structures of our go language.


The variable is a name given to a storage area that the program can manipulate. The variable is like a container and the name of the variable is like a label that shows what is inside the container. Go is a strongly, statically typed language which means variable types need to be explicitly declared and thus determined at compile time.

var a = 100
const b = "I am a String"
var isTrue = false
Enter fullscreen mode Exit fullscreen mode

Here the var and const are keywords. When a variable is declared with the var keyword can change the value after the declaration. When a variable is declared with the const keyword, the value remains the same throughout the life of the program.

Notice that we are not mentioning the type of the variable in the above examples. When we declare a variable without specifying any type, The variable type is inferred from the value of the right-hand side which is known as Type inference.

we can specify the type of the variable as follows

var a int = 200
const b string = "I am Mr.B"
var isBool bool
var price int
var school string
// till now the values of isBool => false, school => "", price => 0
// later point in the programming.

isBool = true
price = 456
school = "New Bay School"
Enter fullscreen mode Exit fullscreen mode

When we don't want to assign the variable value while declaring, we must mention the type of the variable. Every variable declared should be used at least once in the program, the compiler throws an error. The value of the constants should be known by compile time. The values of var can be known at run time.

package main
func main(){
    a := 100
Enter fullscreen mode Exit fullscreen mode

the above line a:=100 is a shorthand notation of var a = 100 or var a int = 100. the := means the variable is declared and assigned with a right-hand side value. This shorthand representation can only be done inside a function and you cannot use := for the same variable.

Declaring Multiple Variables:

In Go, it is possible to declare multiple variables in the same line.

var a, b, c int = 1, 2, 3
Enter fullscreen mode Exit fullscreen mode
var a, b = "Hello" , 3
c, d : = false , "World !"
Enter fullscreen mode Exit fullscreen mode

Multiple variable declarations can be grouped.

var (
    a int
    b int = 1
    c string = "Go Gophers!"
Enter fullscreen mode Exit fullscreen mode


The data type specifies the type and size of the variable values. There are 3 basic basic types in go:

  • bool: represents a boolean value either true or false.
var A bool //'A' will have a false value by default.
A = true
var B = false
C := true
Enter fullscreen mode Exit fullscreen mode
  • Numeric: represents an Integer type, floating type and complex type.

Integer: An integer is used to store a whole number without decimal points. By default integer is int which takes the appropriate size of the machine i.e., 32 bits for a 32-bit machine and 64 bits for a 64-bit machine.

There is also uint which is known as unsigned integer also follows the above rule.

We can give explicit length with int32, or uint32

The list of (signed and unsigned) integer data types => int8, int16, int32, int64, byte, uint8, uint16, uint32, uint64 with byte being the alias for uint8 .

Type Size Range
int Depends on the platform. -2147483648 to 2147483647 in 32-bit systems and
-9223372036854775808 to 9223372036854775807 in 64-bit systems
int8 8-bit -128 to 127
int16 16-bit -32768 to 32767
int32 32-bit -2147483648 to 2147483647
int64 64-bit -9223372036854775808 to 9223372036854775807
uint Depends on the platform. 0 to 4294967295 in 32-bit systems and
0 to 18446744073709551615 in 64-bit systems
uint8 8-bit 0 to 255
uint16 16-bit 0 to 65535
uint32 32-bit 0 to 4294967295
uint64 64-bit 0 to 18446744073709551615
var x uint = 32
var a int = -32
var b uint32 = 4222222
var c int64 = -9999999999
Enter fullscreen mode Exit fullscreen mode

Float: The float data type is used to store numbers with decimal points. To represent floats we need to use float32 and float64 (there is no float type

Type Size Range
float32 32 bits -3.4e+38 to 3.4e+38.
float64 64 bits -1.7e+308 to +1.7e+308.
var a float32 = 123.65
var b float32 = 3.4e+38
var c float64 = 1.7e+308
Enter fullscreen mode Exit fullscreen mode

Complex: Go has native support for complex numbers. Type complex128 (64-bit real and imaginary parts) or type complex64 (32-bit real and imaginary parts).

c1 := complex(10, 11) // constructor init
c2 := 10 + 11i        // complex number init syntax
Enter fullscreen mode Exit fullscreen mode
  • String: represents a string value.

    s := "Hello World !"
    var newString = "New World"
    var nextString string = "next Go"

    Strings in Go are a sequence of UTF-8 characters enclosed in double quotes (“). If you use the single quote (‘) you mean one character (encoded in UTF-8) — which is not a string in Go.


Keywords are reserved words in every language. There are 25 keywords available in the Go language. These keywords were pre-defined inside the go language library and cannot be used as identifiers inside the go program. Keywords cannot be used as variable names or constants.



























Learning GO book

Go variables by Tutorial point

Identifiers and Keywords by coding ninjas

variables and constant in go

Difference between := and = operators in Go

Type inference in go

Data types in Go

go multiple variables

Integer data types

complex numbers

Top comments (0)