capypad
0 day streak
go / beginner
Snippet

Packages and Imports in Go

Go organizes code into packages. The main package is the entry point of any executable program. The import statement lets you include functionality from other packages like fmt for formatted I/O and math for mathematical operations. Go's standard library is extensive and covers most common programming needs.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
package main
 
import (
"fmt"
"math"
)
 
func main() {
fmt.Println("Square root of 16:", math.Sqrt(16))
fmt.Println("Pi value:", math.Pi)
}
Breakdown
1
package main
Declares this file belongs to the main package, required for executables
2
import ( "fmt" "math" )
Imports the fmt package for printing and math for mathematical functions
3
math.Sqrt(16)
Calls the Sqrt function from the math package to calculate square root
4
math.Pi
Accesses the Pi constant from the math package