mdawar.dev

A blog about programming, Web development, Open Source, Linux and DevOps.

Go - Exports

An identifier may be exported to permit access to it from another package.

An identifier is exported only if:

  1. The first character of its name is an uppercase letter
  2. It is declared in the package block or it is a field name or method name
go
package example
// Identifiers that start with an uppercase letter are exported.
// They can be referred to when the package is imported.
var ExportedVariable int = 10
// This variables is not exported and cannot be referenced.
// Only accessible in the declaring package.
var localVariable string = "Hello World!"
// Exported function.
func Hello() string {
return localVariable
}

Using the exported identifiers:

go
package main
import (
"fmt"
"example"
)
func main() {
// Access an exported variable.
fmt.Println(example.ExportedVariable)
// Access an exported function.
fmt.Println(example.Hello())
}