#03 Variables & Constants

#03 Variables & Constants

·

3 min read

Table of contents

Variable

Variables are for storing data values. In Golang there are available data types for variables are string, int, boolean, and float32.

  • There are 2 ways to declare the variable

  • First way is to declare a variable using the var keyword

  • Second way is to declare a variable using the := sign

Syntax

var variablename type = value

Declaring variable needs to specify with type or value.

Example:

package main

import ("fmt")

func main() {

  var a

  fmt.Println(a)
    // output: syntax error: unexpected newline, expecting type
}

If we define the variable without type it will return an error, which means when we don’t want to provide value it requires defining the type of the variable. The correct way to write variable without value is

package main

import ("fmt")

func main() {

  var a int

  fmt.Println(a)
    // output: 0
}

Now coming back to the var keyword to how to declare a variable with the syntax:

package main

import ("fmt")

func main() {
    var variable_1 string = "Hello world!"
    fmt.Println(variable_1)

    // output: Hello world! 
}

Another way is to declare a variable with the := sign, It’s syntax is

variablename := value

When a programmer will use the := sign they will need to assign a value of the variable or not it will return a program error.

package main
import ("fmt")

func main() {

  a :=

  fmt.Println(a)
    // output: assignment mismatch: 1 variable but fmt.Println returns 2 values
    // output: undefined: a
}

Writing an example program to work with := sign

package main
import ("fmt")

func main() {

  a := true

  fmt.Printf("Value: %t, Type: %T", a, a)
    // output: Value: true, Type: bool
}

You can check the format type from fmt formatting. There is another way to declare variables with the below syntax

var variablename = value

Above syntax and := sign type variable is inferred meaning that the compiler decides the type of the variable, based on the value of the variable.

Example:

package main

import ("fmt")

func main() {
    var var_1 string = "String 1"
    var var_2 = "String 2"
    var_3 := "String 3"

    fmt.Printf("%s, %s, %s", var_1, var_2, var_3)
    // Output: String 1, String 2, String 3
}

There are differences between var and :=

var:=
Can be used inside and outside functionsCan only be used inside functions
Variable declaration and value assignment can be done separatelyVariable declaration and value assignment can not be done separately

Constant

The const keyword declares the variable as "constant” that’s means the constant variable is only read-only and unchangeable.

  • A constant variable name in uppercase letters for easy identification from others variables.
  • A constant can be declared inside a function or outside of the function.

Example Syntax:

const CONSTNAME type = value

Example:

package main

import ("fmt")

const PI = 3.14

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

Two types of constant

  • Typed constant
  • Untyped constant

The typed constant is basically declaring the data type for the constant.

Example:

package main

import ("fmt")

const a_var int = 10

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

The Untyped constant is not declaring the data type for the constant.

Example:

package main

import ("fmt")

const b_var = 20

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