[Golang] Variables

2021-09-22 hit count image

Let's see the types of variables and how to use variables in Golang.

Outline

In this blog post, I will introduce the types of variables, and how to use the variables to make programs in Golang.

you can see full source code on the link below.

Variables

To see how to use variables, create the main.go file and modify it like the below.

package main

import "fmt"

func main() {
  var num int = 1
  var message string = "Hello, World!"
  fmt.Println(num, message)

  num = 2
  message = "Hello, Universe!"
  fmt.Println(num, message)
}

After modifying, execute the command below to run the Golang program.

go run main.go

And the, you can see the result like the below.

1 Hello, World!
2 Hello, Universe!

Define variable

You can define the variables like the below in Golang. In Golang, if you don’t use the variable that you defined, the compile error occurs.

// var NAME TYPE = INITIAL_VALUE
var num int = 1

When you define the variable, you should use the var keyword and specify the type of the variable.

You can alos define the variable without the initial value like the below.

var num int

Unlike the other languages, if you don’t assign the value, the default value is assigned instead of null.

  • All integer types(int, int8…): 0
  • All real types(float, float8…): 0.0
  • bool: false
  • string: ""
  • others: nil

When you set the initial value, you can omit the type of the variable like the below.

var num = 10

When you omit the type of the variable, Golang infers the type of the variable.

Lastly, you can use the short variable declaration(:=) to define the variable like the below.

num := 10

Defined type

In Golang, you can alias the type by using the type keyword. This is called Defined type in Golang.

type myInt int

I just define the type myInt as the alias of the type int that is provided by Golang. myInt has the same memory size of int and does same role as int, but It exists as a separate type from the int type.

var myNum myInt = 10
var num int = 10

fmt.Printf("%T\n", myNum)
fmt.Printf("%T\n", num)

If you write the program like above, you can see the result like the below.

main.myInt
int

In Golang, if the types are different, you can’t do operation them. So if you want to operate the variables, you should convert the type of the variables.

Variable types

You can use the following types to define the variables.

  • uint8, uint16, uint32, uint64
  • int8, int16, int32, int64
  • float32, float64
  • byte: alias of uint8
  • rune: to store a character of UTF8 type, alias of int32
  • int: alias of int32
  • uint: alias of uint32
  • bool
  • string
  • array
  • slice
  • structure
  • pointer
  • function type
  • map
  • interface
  • channel

Convert type

As I said, you can’t operate the variables that have different types. So, to operate the different types of the variables, you can convert the type of the variables. In Golang, This is called Type Conversion.

var num1 int = 10
var num2 int32 = 10

// fmt.Println(num1 + num2) // ERROR!!
fmt.Println(num1 + int(num2))

var num3 float32 = 10.32
fmt.Println(num1 + int(num3))

If you try to operate the variables that have different types, the compile error occurs. At this time, you can use Type Conversion to operate them.

When you execute the code above, you can get the result like the below.

20
20

Variable scope

In Golang there is variable scope. You can only use the varible that you defined in the same scope. Scopes are expressed using curly braces({}).

func main() {
  num1 := 10

  {
    num2 := 20
    fmt.Println(num2)
  }

  fmt.Println(num1)
  // fmt.Println(num2) // ERROR!!
}

You can use num1 in the main function scope, but you can only use num2 in the new scope. So, the fmt.Println(num2) code on the bottom is called outside of the scope, so the compile error occurs.

Package global variable

All source codes belong to the certain package in Golang. If you define the variable out of the function, you can define the package global variable that you can use in the whole package.

var num int = 10

func main() {
    fmt.Println(num)
}

Package global variables can be used anywhere in the same package.

Blank identifier

In Golang, if you define the variables, you should use them. When a variable must be assigned but not used, you can use the blank identifier(_) to avoid the compile error.

func main() {
  tmp := make(map[string]string)

  tmp["name"] = "John"
  tmp["job"] = "Programmer"

  fmt.Println(tmp)

  for k, v := range tmp {
    fmt.Println(k, v)
  }

  for k := range tmp {
    fmt.Println(k)
  }

  for _, v := range tmp {
    fmt.Println(v)
  }
}

The example above shows various ways to use the map type after defining the variable. the map can be created using make, and then everything in map can be printed on the screen using range and for.

You can use range to get the key and value of a map at once. Sometimes you want to use both key and value, but sometimes you want to use only value.

// ERROR!!
// for k, v := range tmp {
//     fmt.Println(v)
// }
for _, v := range tmp {
    fmt.Println(v)
}

Like this, when a variable must be assigned but not used, you can use the blank identifier to avoid the compile error.

Completed

We’ve seen the types of variables and how to define and use the variables in Golang. Parts not explained here (how to use map, slice, etc.) will be explained in more detail in other blog posts.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts