Skip to main content

Command Palette

Search for a command to run...

Overview of Functions in Go

Updated
3 min read
Overview of Functions in Go
P

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 main package at the top.

  • We import the fmt package to enable formatted I/O functions.

  • Inside the main function, 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 MixedCaps or mixedCaps for multi-word names instead of underscores.

  • Functions that return an object should be named as nouns, without a Get prefix.

  • For functions that modify an object, use a SetName format.

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:

  1. Create a function named PrintMessage that accepts a parameter message of type string.

  2. Inside the function, use println to output the message.

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.

11 views

Learn GO

Part 11 of 13

In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!

Up next

Understanding Packages in Go

In Go, packages are a key component of organizing and structuring code. Think of them like libraries, modules, or namespaces in other programming languages. Packages help you group related code together. When you build a Go program, it consists of on...

More from this blog

Pavan Chindukuri

18 posts