go / beginner
Snippet
Packages and Importing Modules
Go uses packages to organize code into modular units. The import statement brings in external packages from the standard library or third-party sources. Packages like fmt for formatted I/O, math for mathematical operations, and strings for string manipulation are commonly used. Every Go program starts with package main and a main() function.
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
package mainimport ("fmt""math""strings")func main() {fmt.Println("Square root of 16:", math.Sqrt(16))fmt.Println("Uppercase:", strings.ToUpper("hello"))}
Breakdown
1
package main
Declares this file belongs to the main package, required for executable programs
2
import ( ... )
Import block allowing multiple packages to be included at once
3
fmt.Println(...)
Println from fmt package outputs text followed by a newline
4
math.Sqrt(16)
Sqrt function from math package calculates square root
5
strings.ToUpper(...)
ToUpper from strings package converts string to uppercase