Menu Close

Go – Constant in go golang

In this article, We are going to learn about constant in go golang. Constant is a variable with fixed value in go golang.

This fixed value is also called literals. Constant value does not change during program execution once it is declared. If we do re-assign constant value to different value. Then it would be compiler error.

Declaring a constant value:

We declare a constant by using const keyword.

We can declare constant (const) in multiple ways which are given below:

1:)

Syntax:

const variable_name = value

or

// specifying data type is optional.
const variable_name type = value

Example:

 const name string = “John”

 const roll_number int = 100
 
 const avg float64 = 10.23 

or

 const name = “John”

 const roll_number = 100
 
 const avg = 10.23 

2:) We can also declare multiple constant variables in a single line .

Syntax:

const variable_name_1, variable_name_2, variable_name_3 = value_1, value_2, value_3 

Example:

const name, roll_number, avg = "John", 100, 10.23

3:)

Syntax:

 const (
     const variable_name_1 type = value_1
     const variable_name_2 type = value_2
     const variable_name_3 type = value_3
 ) 

Example:

 const (
      const name string = “John”
      const roll_number int = 100
      const avg float64 = 10.23
 ) 

4:) Using iota keyword:

 iota is a keyword in go Golang. It is used to declare enumerated constants. It’s starting value is 0 and incremented by 1 each time. 

Example:

 const (
     x = iota    //    x == 0
     y = iota    //    y == 1
     z = iota    //    z == 2
 )
 
Here, value of x, y and z are 0, 1, 2 respectively.

 Or

 const (
     x = iota //     x == 0
     y        //     y == 1 (implicitly incremented value by 1)
     z        //     z == 2 (implicitly incremented value by 1)
 ) 

Here, value of x, y and z are also 0, 1, 2 respectively.

To skip value in iota, we use underscore (_) i.e blank identifier.

Example:

 const (
     x = iota //     x == 0
     _            //  (implicitly _ = iota + 1 )
     y            //     y == 2 (implicitly incremented value by 1)
     z           //     z == 3 (implicitly incremented value by 1)
 ) 

Here, y and z has 2 and 3 value respectively. 

Example with code:

package main

import "fmt"

func main() {

    const (
        x = iota
        y
        z
    )

    fmt.Printf("x: %d\n", x)
    fmt.Printf("y: %d\n", y)
    fmt.Printf("z: %d", z)
}

Output:

x: 0
y: 1
z: 2

Type of constant :

There are many types of constant which are given below:

  • Integer constants
  • floating-point constants
  • boolean constants
  • rune constants
  • complex constants
  • string constants

Integer constants

Integer constants are given below:

 100         /* valid. It is decimal number */
 0xabc      /* valid. It is hexadecimal number */
 012        /* valid. It is octal number */
 038         /* Invalid: 8 is not an octal digit */ 
package main

import "fmt"

func main() {

    var a, b, c int

    a = 100
    b = 0xabc
    c = 012

    fmt.Printf("Type: %T, value is: %v\n", a, a)
    fmt.Printf("Type: %T, value is: %v\n", b, b)
    fmt.Printf("Type: %T, value is: %v", c, c)
}

Output:

 Type: int, value is: 100
 Type: int, value is: 2748
 Type: int, value is: 10

floating-point constants

floating-point constants are given below:

 1.342       // valid
 1234E-4     // valid
 110E        // invalid:  incomplete exponent
 110f        // invalid:  no decimal or exponent
 .e66        // invalid: missing integer or fraction 
package main

import "fmt"

func main() {

    var a, b float64

    a = 1.342
    b = 1234E-4

    fmt.Printf("Type: %T, value is: %v\n", a, a)
    fmt.Printf("Type: %T, value is: %v", b, b)
}

Output:

Type: float64, value is: 1.342
Type: float64, value is: 0.1234

boolean constants

There are two types of boolean constants value which are given below:

1:) true
2:) false 
package main

import "fmt"

func main() {

    var a, b bool

    a = true
    b = false

    fmt.Printf("Type: %T, value is: %v\n", a, a)
    fmt.Printf("Type: %T, value is: %v", b, b)
}

Output:

Type: bool, value is: true
Type: bool, value is: false

rune constant

rune literals are just 32-bit integer values. It means, each rune takes 4 byte. It is untyped constant so their type can change. It represents unicode code point. To declare rune, we use single quote (‘ ‘) like ‘a’.

So rune literal ‘a’ is actually the number 97.

package main

import "fmt"

func main() {

    var a rune

    a = 'a'

    fmt.Printf("Type: %T, value is: %v", a, a)
}

Output:

Type: int32, value is: 97

String constants

String constants are given below:

"Hello"
"Golang" 
package main

import "fmt"

func main() {

    var a, b string

    a = "hello"
    b = "Golang"

    fmt.Printf("Type: %T, value is: %v\n", a, a)
    fmt.Printf("Type: %T, value is: %v", b, b)
}

Output:

Type: string, value is: hello
Type: string, value is: Golang

complex constants

complex constants are given below:

2.7i
3 + 5i 
package main

import "fmt"

func main() {

    a := 2.7i
    b := 3 + 5i

    fmt.Printf("Type: %T, value is: %v\n", a, a)
    fmt.Printf("Type: %T, value is: %v", b, b)
}

Output:

Type: complex128, value is: (0+2.7i)
Type: complex128, value is: (3+5i)

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

 https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in golang

Leave a Reply

Your email address will not be published.

Contact Us