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

Top comments (0)