ADVERTISEMENT
ADVERTISEMENT

Golang Constants

Golang | Constants: In this tutorial, we are going to learn about creating, using constants in Golang.
Submitted by IncludeHelp, on July 23, 2021

Just like other programming languages, in Golang, a constant is the name of a fixed value. Constant's value cannot be changed.

Declaring constants

To declare a constant in Golang, we use the const keyword followed by an identifier (constant name), type, and the constant value. The constant value must be assigned during the declaration of the constant.

Syntax:

const constant_name type = constant_value  
const constant_name = constant_value

Note: We can also omit the type during the constant declaration, the compiler assigns the type of the value to the constant.

Example:

package main

import (
	"fmt"
)

const DEFAULT_NAME string = "Not available"
const DEFAULT_VALUE = 0

func main() {
	fmt.Println("The value of DEFAULT_NAME is", DEFAULT_NAME)
	fmt.Println("The value of DEFAULT_VALUE is", DEFAULT_VALUE)
}

Output:

The value of DEFAULT_NAME is Not available
The value of DEFAULT_VALUE is 0

Declaring multiple constants

To declare multiple constants together – follow the below syntax,

Example:

package main

import (
	"fmt"
)

const (
	DEFAULT_NAME  string = "Not available"
	DEFAULT_VALUE        = 0
)

func main() {
	fmt.Println("The value of DEFAULT_NAME is", DEFAULT_NAME)
	fmt.Println("The value of DEFAULT_VALUE is", DEFAULT_VALUE)
}

Output:

The value of DEFAULT_NAME is Not available
The value of DEFAULT_VALUE is 0

What happens if we don't assign the value while constant declaration?

If we don't assign the value to the constant while declaration, the constant will not be declared and the program will generate an error.

package main

import (
	"fmt"
)

const DEFAULT_NAME string
const DEFAULT_VALUE

func main() {
	fmt.Println("The value of DEFAULT_NAME is", DEFAULT_NAME)
	fmt.Println("The value of DEFAULT_VALUE is", DEFAULT_VALUE)
}

Output:

./prog.go:7:20: const declaration cannot have type without expression
./prog.go:7:20: missing value in const declaration
./prog.go:8:7: missing value in const declaration
./prog.go:11:46: undefined: DEFAULT_NAME
./prog.go:12:47: undefined: DEFAULT_VALUE

What happens if we change the value of a constant after the declaration?

We cannot change the value of a constant after declaring it. If we try to change the value of a constant after the declaration, the program will generate an error.

package main

import (
	"fmt"
)

const DEFAULT_NAME string = "Not available"
const DEFAULT_VALUE = 0

func main() {
	fmt.Println("The value of DEFAULT_NAME is", DEFAULT_NAME)
	fmt.Println("The value of DEFAULT_VALUE is", DEFAULT_VALUE)

	DEFAULT_VALUE = 100
	fmt.Println("The value of DEFAULT_VALUE is", DEFAULT_VALUE)
}

Output:

./prog.go:14:16: cannot assign to DEFAULT_VALUE (declared const)

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.