Declare and access exported identifiers - Pkg
// Package counters provides alert counter support.
package counters
// AlertCounter is an exported named type that
// contains an integer counter for alerts.
type AlertCounter int
Declare and access exported identifiers - Main
// Sample program to show how to access an exported identifier.
package main
import (
"fmt"
"github.com/ardanlabs/gotraining/topics/go/language/exporting/example1/counters"
)
func main() {
// Create a variable of the exported type and initialize the value to 10.
counter := counters.AlertCounter(10)
fmt.Printf("Counter: %d
", counter)
}
Declare unexported identifiers and restrictions - Pkg (
// Package counters provides alert counter support.
package counters
// alertCounter is an unexported named type that
// contains an integer counter for alerts.
type alertCounter int
Declare unexported identifiers and restrictions - Main
// Sample program to show how the program can't access an
// unexported identifier from another package.
package main
import (
"fmt"
"github.com/ardanlabs/gotraining/topics/go/language/exporting/example2/counters"
)
func main() {
// Create a variable of the unexported type and initialize the value to 10.
counter := counters.alertCounter(10)
// ./example2.go:17: cannot refer to unexported name counters.alertCounter
// ./example2.go:17: undefined: counters.alertCounter
fmt.Printf("Counter: %d
", counter)
}
Access values of unexported identifiers - Pkg
// Package counters provides alert counter support.
package counters
// alertCounter is an unexported named type that
// contains an integer counter for alerts.
type alertCounter int
// New creates and returns values of the unexported type alertCounter.
func New(value int) alertCounter {
return alertCounter(value)
}
Access values of unexported identifiers - Main
// Sample program to show how the program can access a value
// of an unexported identifier from another package.
package main
import (
"fmt"
"github.com/ardanlabs/gotraining/topics/go/language/exporting/example3/counters"
)
func main() {
// Create a variable of the unexported type using the exported
// New function from the package counters.
counter := counters.New(10)
fmt.Printf("Counter: %d
", counter)
}
Unexported struct type fields - Pkg
// Package users provides support for user management.
package users
// User represents information about a user.
type User struct {
Name string
ID int
password string
}
Unexported struct type fields - Main
// Sample program to show how unexported fields from an exported struct
// type can't be accessed directly.
package main
import (
"fmt"
"github.com/ardanlabs/gotraining/topics/go/language/exporting/example4/users"
)
func main() {
// Create a value of type User from the users package.
u := users.User{
Name: "Chole",
ID: 10,
password: "xxxx",
}
// ./example4.go:21: unknown users.User field 'password' in struct literal
fmt.Printf("User: %#v
", u)
}
Unexported embedded types - Pkg
// Package users provides support for user management.
package users
// user represents information about a user.
type user struct {
Name string
ID int
}
// Manager represents information about a manager.
type Manager struct {
Title string
user
}
Unexported embedded types - Main
// Sample program to show how to create values from exported types with
// embedded unexported types.
package main
import (
"fmt"
"github.com/ardanlabs/gotraining/topics/go/language/exporting/example5/users"
)
func main() {
// Create a value of type Manager from the users package.
u := users.Manager{
Title: "Dev Manager",
}
// Set the exported fields from the unexported user inner type.
u.Name = "Chole"
u.ID = 10
fmt.Printf("User: %#v
", u)
}