Understanding Go Functions, Methods, Interfaces, and Core Commands
This article explains Go's function syntax, closures, method definitions on structs, interface mechanics, and provides a concise overview of essential Go commands such as build, install, get, doc, test, list, and fix, illustrated with code examples and output results.
Functions
Go functions may have no parameters or multiple parameters. When consecutive functions share the same named parameter types, the type can be omitted for all but the last parameter. A function whose name starts with an uppercase letter is exported. Functions can return any number of values, and return values may be named, acting as variables that can be returned without explicit values.
func add(x, y int) int {
return x + y
}
func swap(x, y string) (string, string) {
return y, x
}
func division(dividend, divisor int) (quotient, remainder int) {
quotient = dividend / divisor
remainder = dividend - quotient * divisor
return
}Functions are first‑class values: they can be passed as arguments and returned from other functions.
// compute accepts a function as a parameter
func compute(fn func(float64, float64) float64) float64 {
return fn(3, 4)
}Closures
A closure pairs a function with its surrounding environment, capturing external variables so they can be accessed and modified later. In Go, a closure is typically created by returning an inner function from an outer function. If the outer variable has local visibility, the closure’s environment is closed; otherwise, changes to a global variable affect the closure.
func test_1(x int) func() {
return func() {
x++
fmt.Println(x)
}
}
func test_2(x int) func() {
sum := 0
return func() {
sum += x
fmt.Println(x, sum)
}
}
func test_3(x int) func(int) int {
sum := 0
return func(y int) int {
sum += x * y
return sum
}
}
func main() {
test_1(1)()
test_2(1)()
// each closure has a distinct external environment
for i := 0; i < 5; i++ {
fmt.Printf("%d ", test_3(1)(i))
}
fmt.Println()
// all closures share the same external environment
tmp := test_3(1)
for i := 0; i < 5; i++ {
fmt.Printf("%d ", tmp(i))
}
fmt.Println()
}Program output:
2
1 1
1 1
0 1 2 3 4
0 1 3 6 10Methods
Go does not have classes, but methods can be defined on any type, most commonly on structs. The receiver appears between the func keyword and the method name. A method with a value receiver cannot modify the receiver; a pointer receiver can modify the underlying value. Methods with pointer receivers can be called on both pointer and value instances.
type Vertex struct {
X, Y float64
}
func (v Vertex) distance() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
} func (v *Vertex) Move_1(dx, dy float64) {
v.X += dx
v.Y += dy
}
func (v Vertex) Move_2(dx, dy float64) {
v.X += dx
v.Y += dy
}
func Move_3(v *Vertex, dx, dy float64) {
v.X += dx
v.Y += dy
}
func Move_4(v Vertex, dx, dy float64) {
v.X += dx
v.Y += dy
}
func main() {
var v Vertex
v.X = 0
v.Y = 0
v.Move_1(1, 1)
fmt.Println(v.X, v.Y) // 1 1
p := &v
p.Move_1(1, 1)
fmt.Println(v.X, v.Y) // 2 2
v.Move_2(1, 1)
fmt.Println(v.X, v.Y) // 2 2 (no change)
Move_3(&v, 1, 1)
fmt.Println(v.X, v.Y) // 3 3
Move_4(v, 1, 1)
fmt.Println(v.X, v.Y) // 3 3 (value copy)
}Program output:
1 1
2 2
2 2
3 3
3 3Interfaces
An interface is a set of method signatures; any type that implements all those methods satisfies the interface implicitly—no implements keyword is required. The empty interface ( interface{}) can hold values of any type.
type MyFloat float64
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
type Vertex struct {
X, Y float64
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type Abser interface {
Abs() float64
}
func main() {
var a Abser
f := MyFloat(-math.Sqrt2)
v := Vertex{3, 4}
a = f // MyFloat implements Abs
a = &v // *Vertex implements Abs
}Empty‑interface example with type assertions:
var i interface{} = "hello"
s := i.(string) // succeeds, s == "hello"
fmt.Println(s)
s, ok := i.(string) // ok == true
fmt.Println(s, ok)
f, ok := i.(float64) // ok == false, f == 0
fmt.Println(f, ok)
f = i.(float64) // panic: interface conversion: interface {} is string, not float64Program output:
hello
hello true
0 false
panic: interface conversion: interface {} is string, not float64Core Go Commands
go build compiles the specified package and, if necessary, its dependencies.
go install compiles and installs the package and its dependencies.
go get downloads or updates the named package and its dependencies, then compiles and installs them.
go doc prints the documentation attached to a Go entity identified by its name.
godoc is a powerful tool for displaying package documentation; it became a built‑in command starting with Go 1.5.
go test runs tests for a package.
go list lists information about the specified packages.
go fix (or go tool fix) rewrites Go source code to update deprecated constructs to the current language version.
These commands form the essential workflow for building, testing, documenting, and maintaining Go projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
