Overview of Functions in Go

I am a self-taught Fullstack Web and Mobile App developer with over 4 years of experience. I have built a few apps using React and React Native with a backend in Nest.JS and Express.JS
In this lesson, we’ll walk through how to write a simple function in Go, covering the basics of functions, writing a "Hello World" program, understanding comments, and best practices for naming things in Go. You’ll also have the opportunity to try out a simple function yourself.
Functions in Go
A function in Go is declared with the following basic structure:
func functionName() {
// function body
}
The parentheses () can include zero or more parameters, separated by commas. Each parameter must have a specified type.
The main Function
Every Go program starts with the main function. It's the entry point for the application. Without it, you'll encounter an error: undefined: main.main. The main function does not take any parameters and does not return a value. If you try to give it parameters or a return type, you’ll get another error: func main must have no arguments and no return values.
Here’s the syntax for declaring a function with parameters:
func funcName(param1 type1, param2 type2) {
// function body
}
If the function returns a value, the return type is specified after the parameter list:
func funcName(param1 type1, param2 type2) type1 {
// return something of type1
}
For multiple return values, it looks like this:
func funcName(param1 type1, param2 type2) (ret1 type1, ret2 type2) {
// function body
}
You can also write small, single-line functions:
func Sum(a, b int) int { return a + b }
Writing the main Function
The main function acts as the starting point of your Go program. Let’s write a simple program that prints "Hello World".
Hello World Program
Here’s a basic "Hello World" program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
In this example:
We define the
mainpackage at the top.We import the
fmtpackage to enable formatted I/O functions.Inside the
mainfunction,fmt.Println("Hello World")is used to print the message to the console.
Comments in Go
Comments are non-executable lines of text in your code. They are meant to help explain the code and are ignored during compilation. Go supports two types of comments:
Single-line comments start with
//.Multi-line (block) comments are enclosed in
/* */, but nesting block comments is not allowed.
Example:
// This is a single-line comment
/*
This is a multi-line comment.
It can span multiple lines.
*/
Naming Conventions in Go
Go emphasizes simplicity and readability. This means names should be short, clear, and descriptive. Here are a few guidelines for naming in Go:
Avoid overly long or complex names.
Don’t include the package name in your function or variable names.
Use
MixedCapsormixedCapsfor multi-word names instead of underscores.Functions that return an object should be named as nouns, without a
Getprefix.For functions that modify an object, use a
SetNameformat.
Try It Yourself: Implementing a Simple Function
Now, it’s time for you to implement a basic function in Go. Let’s create a function that prints a message to the console. Here’s how:
Create a function named
PrintMessagethat accepts a parametermessageof typestring.Inside the function, use
printlnto output themessage.
Here’s an example:
package main
func PrintMessage(message string) {
println(message)
}
func main() {
PrintMessage("Hello, Go!")
}
This simple function takes a message as input and prints it to the console. You can modify it to print any message you want!
Try it out here - https://go.dev/play/p/MZhPqlGKrcI
With this basic understanding of functions in Go, you’re now ready to explore more complex aspects of Go programming, starting with elementary data types.




