Go constants

Go supports constants, which are variables that cannot change their values. Constants in Go are defined with the help of the const keyword.

Generally speaking, constants are usually global variables. Thus, you might rethink your approach if you find yourself defining too many constant variables with a local scope!

The main benefit that you get from using constants in your programs is the guarantee that their value will not change during program execution!

Strictly speaking, the value of a constant variable is defined at compile time-not at run time. Behind the scenes, Go uses Boolean, string, or number as the type for storing a constant variable because this gives Go more flexibility when dealing with constants!

You can define a new constant as follows:

const HEIGHT = 200 

Additionally, if you want to declare many constants at once, mainly because they are related to each other, you can use the following notation:

const ( 
    C1 = "C1C1C1" 
    C2 = "C2C2C2" 
    C3 = "C3C3C3" 
) 

Please note that the Go compiler considers the results of all operations applied to constants as constants. However, if a constant is part of a larger expression, this will not be the case.

And now for something completely different! The following three variable declarations mean exactly the same thing in Go:

    s1 := "My String" 
    var s2 = "My String" 
    var s3 string = "My String" 

However, as none of these variable declarations contains the const keyword in its declaration, none of them is a constant. This does not mean that you cannot define two constants in a similar way as follows:

    const s1 = "My String" 
    const s2 string = "My String" 

Although both s1 and s2 are constants, s2 comes with a type declaration (string), which makes its declaration more restrictive than the declaration of s1. This is because a typed Go constant must follow all of the strict rules of a typed Go variable. On the other hand, a constant without a type need not follow all of the strict rules of a typed variable, which means that it can be mixed with expressions more liberally. Additionally, even constants without a type have a default type that is used when, and only when, no other type information is available. The main reason for this behavior is that as you do not know how a constant will be used. In that case, you do not want to use all of the available Go rules. A simple example is the definition of a numeric constant such as const value = 123. As you might use the value constant in many expressions, declaring a type would make your job much more difficult. Look at the following Go code:

    const s1 = 123 
    const s2 float64 = 123 
 
    var v1 float32 = s1 * 12 
    var v2 float32 = s2 * 12 

Although the compiler will not have a problem with the definition of v1, the code used for the definition of v2 will not compile because s2 and v2 have different types:

$ go run a.go
# command-line-arguments
./a.go:12:6: cannot use s2 * 12 (type float64) as type float32 in 
assignment
As general advice, if you are using lots of constants in your programs, it might be a good idea to gather all of them in a Go package.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156