exporting/

directory
v0.0.0-...-b3f521c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2017 License: Apache-2.0

README

Exporting

Packages contain the basic unit of compiled code. They define a scope for the identifiers that are declared within them. Exporting is not the same as public and private semantics in other languages. But exporting is how we provide encapsulation in Go.

Notes

  • Code in go is complied into packages and then linked together.
  • Identifiers are exported (or remain unexported) based on letter-case.
  • We import packages to access exported identifiers.
  • Any package can use a value of an unexported type, but this is annoying to use.

http://www.goinggo.net/2014/03/exportedunexported-identifiers-in-go.html

Code Review

Declare and access exported identifiers - Pkg (Go Playground)
Declare and access exported identifiers - Main (Go Playground)

Declare unexported identifiers and restrictions - Pkg (Go Playground)
Declare unexported identifiers and restrictions - Main (Go Playground)

Access values of unexported identifiers - Pkg (Go Playground)
Access values of unexported identifiers - Main (Go Playground)

Unexported struct type fields - Pkg (Go Playground)
Unexported struct type fields - Main (Go Playground)

Unexported embedded types - Pkg (Go Playground)
Unexported embedded types - Main (Go Playground)

Exercises

Exercise 1

Part A Create a package named toy with a single exported struct type named Toy. Add the exported fields Name and Weight. Then add two unexported fields named onHand and sold. Declare a factory function called New to create values of type toy and accept parameters for the exported fields. Then declare methods that return and update values for the unexported fields.

Part B Create a program that imports the toy package. Use the New function to create a value of type toy. Then use the methods to set the counts and display the field values of that toy value.

Template | Answer


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how to access an exported identifier.
Sample program to show how to access an exported identifier.
counters
Package counters provides alert counter support.
Package counters provides alert counter support.
Sample program to show how the program can't access an unexported identifier from another package.
Sample program to show how the program can't access an unexported identifier from another package.
counters
Package counters provides alert counter support.
Package counters provides alert counter support.
Sample program to show how the program can access a value of an unexported identifier from another package.
Sample program to show how the program can access a value of an unexported identifier from another package.
counters
Package counters provides alert counter support.
Package counters provides alert counter support.
Sample program to show how unexported fields from an exported struct type can't be accessed directly.
Sample program to show how unexported fields from an exported struct type can't be accessed directly.
users
Package users provides support for user management.
Package users provides support for user management.
Sample program to show how to create values from exported types with embedded unexported types.
Sample program to show how to create values from exported types with embedded unexported types.
users
Package users provides support for user management.
Package users provides support for user management.
exercises
exercise1
Create a package named toy with a single exported struct type named Toy.
Create a package named toy with a single exported struct type named Toy.
exercise1/toy
Package toy contains support for managing toy inventory.
Package toy contains support for managing toy inventory.
template1
Create a package named toy with a single exported struct type named Toy.
Create a package named toy with a single exported struct type named Toy.
template1/toy
Package toy contains support for managing toy inventory.
Package toy contains support for managing toy inventory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL