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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

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

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay