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
- Use the Go official package documentation.
- For any package (e.g.,
math
), navigate to its page (e.g., math package) to see a list of exported items. - Exported items are listed with descriptions and usage examples.
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
, typemath.
to see suggestions for all exported members.
- Example: After importing
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
Output:
package math // import "math"
const Pi = 3.141592653589793
Pi is the ratio of a circle's circumference to its diameter.
Top comments (0)