liveBook · Manning

Exported identifier

As explained in Learn Go with Pocket-Sized Projects

An exported symbol is a Go declaration whose identifier begins with an uppercase letter and is accessible from another package.

An exported identifier is a Go declaration whose name begins with an uppercase letter, making it accessible from other packages. Lowercase declarations remain package-local. Packages therefore define a practical visibility boundary: expose the API users need, while retaining freedom to change internal implementation. section 4.1.1

Apply these package and export rules:

  • Export functions, types, variables, constants, or other symbols by starting their names with uppercase letters. section 4.1.1
  • Keep implementation details lowercase when they should remain unavailable outside the package. section 4.1.1
  • Document every exported symbol with a comment whose sentence starts with the symbol’s name. section 4.1.2
  • Keep exported APIs stable for backward compatibility; performance fixes and bug repairs can usually stay behind unexported declarations. section 4.1.1

Exporting a logging type and levels

A library can expose a Logger type and a Level enumeration while keeping other implementation details private:\n\n// Logger is used to log information.\ntype Logger struct {}\n\n// Level represents an available logging level.\ntype Level byte\n\nconst (\n // LevelDebug represents the lowest level of log.\n LevelDebug Level = iota\n LevelInfo\n LevelError\n)\n\nOther packages can use Logger, Level, and the Level constants because their names begin with uppercase letters. section 4.1.1 section 4.1.2

const (
   // LevelDebug represents the lowest level of log, mostly used for 
    // debugging purposes.
   LevelDebug Level = iota
   // LevelInfo represents a logging level that contains information 
    // deemed valuable.
   LevelInfo
   // LevelError represents the highest logging level, only to be used 
    // to trace errors.
   LevelError
)

Inspect the complete exported logging-level declaration, including documentation comments and iota-based constants. section 4.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains naming, documentation, compatibility, and concrete declarations, but does not descend into compiler or linker internals.
Examples
Uses a Logger type, a byte-based Level type, and LevelDebug, LevelInfo, and LevelError constants.
What it assumes
Assumes familiarity with Go declarations and packages while reinforcing package scope and module organization.
What you can do after
Apply Go naming and documentation conventions when designing a package API for external users.
How it connects
Connects packages, modules, visibility, documentation comments, backward compatibility, and enumerations.

Also explained in

Compare how 2 books explain exported identifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.