Constants

suggest change

Go supports constants of character, string, boolean, and numeric values.

Constant basics:

// Greeting is an exported (public) string constant
const Greeting string = "Hello World"

// we can group const declarations
const (
	// years is an unexported (package private) int constant
	years int  = 10
	truth bool = true
)

Constants can be used like variables, except that their value can't change.

An example:

// collection: Essential Go
package main

import (
	"fmt"
	"math"
)

const s string = "constant"

func main() {
	fmt.Println(s) // constant

	// A `const` statement can appear anywhere a `var` statement can.
	const n = 10
	fmt.Println(n)                           // 10
	fmt.Printf("n=%d is of type %T\n", n, n) // n=10 is of type int

	const m float64 = 4.3
	fmt.Println(m) // 4.3

	// An untyped constant takes the type needed by its context.
	// For example, here `math.Sin` expects a `float64`.
	const x = 10
	fmt.Println(math.Sin(x)) // -0.5440211108893699
}
constant
10
n=10 is of type int
4.3
-0.5440211108893699

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents