Getting Started with Go —Functions & Arrays
Hello everyone! Welcome back to our blog, and if you’re a newbie here, well, you’re in for a ride! 🎢 Today, we are going to dive deep into the Go language. So, whether you’re a seasoned coder looking to learn a new skill, or a curious soul dipping your toes in the world of programming, we’ve got you covered! 😉
Previously on …
Functions and Multiple/Named Returns
Let’s start with Functions in Go. Functions in Go are defined using the `func` keyword, followed by the name of the function, input parameters, and return parameters. Go supports multiple returns, which can be very useful. Take a look:
Spiffy, ain’t it? Go also supports named returns. Check it out :
Notice how our function `swap` returns two strings which we’ve named before using them, eliminating ambiguity. It’s all about keeping it simple, folks! 😉
Packages, Imports, and Exports
Let’s start simple. A package in Go is essentially a directory of related Go files. Importing a package is as simple as using the keyword `import` followed by the name of the package.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Pi)
}
But, the name of the exported variable (in this case, Pi) must start with a capital letter- a little quirk to remember when working with Go! The capital letter tells Go that the particular function or variable can be exported.