liveBook · Manning

Exported identifier

As explained in 100 Go Mistakes and How to Avoid Them

An exported element is a Go package declaration whose name begins with an uppercase letter and is accessible from other packages.

An exported identifier is a Go package declaration whose name begins with an uppercase letter, making it accessible to other packages. Exporting defines the package boundary: code in a subdirectory does not inherit access to its parent package, and external code can use only exported elements. section 2.12.2

Exporting an API deliberately

// Customer is a customer representation. type Customer struct{} // ID returns the customer identifier. func (c Customer) ID() string { return "" } The uppercase names Customer and ID are available to clients of the package. Every exported element should have documentation beginning with its name; comments should explain intended use rather than implementation. section 2.15

Use exported identifiers as a small, intentional public surface:

  • Export only what clients need; minimizing exports keeps package responsibilities clearer. section 2.12.2
  • Do not assume that placing packages under a common directory grants special access; net/http can see only exported net elements. section 2.12.2
  • Choose exported methods pragmatically: getters and setters can encapsulate validation, computed values, synchronization, or representation changes, but they are not mandatory in Go. section 2.4
  • Document deprecations with a Deprecated: comment so tools can warn clients about replacement APIs. section 2.15
/net
    /http
        client.go
        ...
    /smtp
        auth.go
        ...
    addrselect.go
    ...

The standard-library-style directory listing makes package boundaries visible: net contains related packages, while net/http and net/smtp remain distinct packages. section 2.12.2

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Defines capitalization-based visibility and documents API practices, but does not descend into compiler or runtime internals.
Examples
Uses exported Customer and ID declarations, net subdirectories, deprecation comments, and Balance getter and setter methods.
What it assumes
Assumes familiarity with Go packages and declarations while establishing package visibility and public-API documentation practices.
What you can do after
Apply Go visibility and documentation conventions when designing a package API.
How it connects
Connects package organization, API documentation, deprecation, encapsulation, and getter or setter design.

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 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.