DEV Community

Cover image for Mastering Go Packages: How to Discover Exported Functionalities
Md Abu Musa
Md Abu Musa

Posted on

Mastering Go Packages: How to Discover Exported Functionalities

To find out which exported functionalities (e.g., functions, constants, variables, types) a Go package provides, you can use the following methods:


1. Official Documentation


2. go doc Command

  • Run the go doc command to see the documentation of a package directly in your terminal.
  • Example:

     go doc math
    

    Output:

     package math // import "math"
    
     Constants:
         Pi = 3.141592653589793 // The mathematical constant π.
     Functions:
         Abs(x float64) float64
         ...
    

3. IDE or Code Editor Features

  • Modern IDEs like GoLand, VS Code, or LiteIDE provide code navigation tools.
  • When you import a package in your Go file, you can use features like IntelliSense or Auto-Completion to see all exported functionalities.
    • Example: After importing math, type math. to see suggestions for all exported members.

4. Source Code

  • Go to the source code of the package to check for exported items. Look for names starting with capital letters.
  • For example, you can find the math package source at its repository:

     https://github.com/golang/go/tree/master/src/math
    

5. godoc Command

  • If you have the godoc tool installed locally, you can view documentation in your browser.
  • Install in ubuntu: sudo apt install golang-golang-x-tools
  • Start a local documentation server:

     godoc -http=:6060
    
  • Open http://localhost:6060 in your browser and navigate to the package.


Example with the math Package

Exported Functionalities

You can find:

  • Constants: Pi, E, etc.
  • Functions: Abs, Sin, Cos, etc.

How to Check (Terminal Example):

go doc math.Pi
Enter fullscreen mode Exit fullscreen mode

Output:

package math // import "math"

const Pi = 3.141592653589793
    Pi is the ratio of a circle's circumference to its diameter.
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay