loading...
Cover image for Exported names in Go

Exported names in Go

bluepaperbirds profile image bluepaperbirds ・1 min read

Many packages have variables (constants). In the Go programming language, exported variable names are a bit different.

Before playing around with Go packages and constants, you should be able to run go programs

Example

For example, if you import the math module you can access some of its variables:

fmt.Println(math.Pi)
fmt.Println(math.E)

These variables reside in the math module. When importing a package, you can refer only to its exported names.

Lets try that in a program

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(math.Pi)
    fmt.Println(math.E)
    fmt.Println(math.Phi)
    fmt.Println(math.SqrtE)
}

If a variable does not start with a capital letter, it is not exported.

So if you'd try

fmt.Println(math.e)

That wouldn't work, because only variables with a capital letter are exported.

Constants

So how do you know which variables exist in a module?

One way to find out is in the golang package docs. Open the name of the package and search for constants.

You can browse some of the main packages here

Discussion

pic
Editor guide