본문 바로가기
Go

[Go] Variable

by llHoYall 2020. 10. 21.

Variable is storage having a value.

Variable Declaration

You can give a name to a variable through variable declaration.

var <name of variable> <type of variable>

If you assign a value to a variable, use = symbol.

You can also assign multiple values to multiple variables.

var count int = 3
var name string = "HoYa"
var width, height float64 = 2.6, 3.2
var count int = 5  // Error: variable can't redeclare.

count = 7
name = 5  // Error: Type mismatch

Declared variables should be used.

Variable Type Inference

When assigning values at the same time as a declaration, the type of variable can be omitted.

In this case, Go infers the type of variable as the assigned value.

var count = 3
var name = "HoYa"
var width, height = 2.6, 3.2

fmt.Println(reflect.TypeOf(count))  // int
fmt.Println(reflect.TypeOf(name))  // string
fmt.Println(reflect.TypeOf(width))  // float64
fmt.Println(reflect.TypeOf(height))  // float64

Zero Initialization

The variable without assigning value is zero-initialized.

var count int
var name string
var width float64
var check bool

fmt.Println(count)  // 0
fmt.Println(name)  // ""
fmt.Println(width)  // 0
fmt.Println(check)  // false

Short Variable Declaration

When declaring a variable, assigning a value at the same time as the declaration usually uses a short variable declaration.

name := "HoYa"
age := 18

fmt.Println("My name is", name, ", and I'm", age, "years old.")
// My name is HoYa , and I'm 18 years old.

You can use short variable declaration when one or more variables are new variables in a short-term variable declaration.

a := 3
a, b := 2, 4
fmt.Println(a, b)
// 2, 4

In the above example, a is assigned a new value and b is newly declared.

Naming Convention

  • Variable names have to start with a character and it should consist of any number of characters and numbers.
  • If the name begins in uppercase, it is exported to the outside and accessible from the external package.
  • If the name begins in lowercase, it is not exported to the outside and only accessible from the same package.
  • You shouldn't use the keyword. If you use a keyword as a name of a variable, it would be overridden.
  • It is recommended to use a camel case.

Variable Scope

All declared variable has its scope.

A declared variable can be accessed anywhere within the scope to which it belongs, but not outside the scope, and an error occurs when trying to access it.

package main

import (
  "fmt"
)

func main() {
  a := 3
	
  if true {
    a = 7  // Can access because it is in the inner block
    b := 3
    fmt.Println(a, b)
  } else {
    a = -5
    b := -4
    fmt.Println(a, b)
  }
  fmt.Println(a, b)  // Error: B cannot access because this is the outer block
}

Blank Identifier

If you don't want to use a variable, how should you do it?

In Go, the declared variable must be used.

In this case, you can use the blank identifier _.

package main

import (
  "bufio"
  "fmt"
  "os"
)

func main() {
  input, _ := bufio.NewReader(os.Stdin).ReadString('\n')
  fmt.Println(input)
}

'Go' 카테고리의 다른 글

[Go] Configure VSCode  (0) 2020.11.28
[Go] User-Defined Type  (0) 2020.11.02
[Go] Basic  (0) 2020.10.26
[Go] Data Types  (0) 2020.10.21
[Go] Getting Started (시작하기)  (0) 2020.08.28

댓글