Getting Started #
Functions are at the core of Golang, they are easy to understand, fun to work with and surprisingly provide a decent amount of flexibility. In this article I aim to teach you the main aspects of functions.
As always, I recommend you to try out all the examples by hand šØš»āš» the more you practice, the more you understand, and the more you retain. Go Playground and GoPlay are good online websites to quickly write and run some go code.
Declaring Functions #
First thing you do when you write GO code is to declare the main
function.
package main
func main() {
}
We declare functions using the func
keyword followed by the name of the function (main
in this case).
A custom hello world function would look something like this.
package main
import "fmt"
// š we declare our custom function
func SayHello() {
fmt.Println("Hello!")
}
func main() {
SayHello() // š calling the `SayHello` function.
}
Function Arguments #
Let's add an argument to our SayHello
function.
package main
import "fmt"
func SayHello(firstName string) { // š declaring an argument
fmt.Println("Hello", firstName)
}
func main() {
SayHello("User") // š calling the function with a value
}
Just like that you can keep adding as many arguments as required.
package main
import "fmt"
func SayHello(firstName string, lastName string, age int) {
fmt.Println(firstName, lastName)
fmt.Println("Age:", age)
}
func main() {
SayHello("First", "Last", 10)
}
If there are two arguments of the same type declared back to back, you can omit the type declaration of the arguments except the last one, so here firstName
and lastName
are declared back to back and have the same type string
, we can omit the type for firstName
.
package main
import "fmt"
func SayHello(firstName, lastName string, age int) {
fmt.Println(firstName, lastName)
fmt.Println("Age:", age)
}
func main() {
SayHello("First", "Last", 10)
}
Variadic Functions #
Go allows you to create functions where you can pass any number of arguments when calling the function. Let's declare a function that will take any number of string
s and print them separately on each line.
package main
import "fmt"
func main() {
PrintLines("Hello", "I", "am a", "GO Programmer")
}
func PrintLines(lines ...string) { // š variadic argument `lines`
// `lines` is of type `[]string` here
for _, line := range lines {
fmt.Println(line)
}
}
You can make an argument a variadic argument using the ...
syntax, here ...string
will accept any number of string
s when PrintLines
is being called.
An important thing to note here is that inside PrintLines
function the type of argument lines
is []string
, so it is a slice of strings. Whenever you use variadic arguments it will be a slice of a type.
ā Note: Variadic argument can only be the final argument of the function, once you have a variadic argument declared you cannot declare anything else after it, so the below code will not compile.
func PrintLines(lines ...string, int num) { // ā cannot define anything after `lines`
for _, line := range lines {
fmt.Println(line)
}
}
// Compilation Error -> `can only use ... with final parameter in list`