Go from Beginner to Expert: A Complete Guide to Learn Golang PART-14

Step-by-Step Guide to understand Functions and Packages in Golang.

Md. Faiyaj Zaman
3 min readMar 9, 2023

--

Hi everyone, welcome back to our series on learning Golang step by step! In this tutorial, we’re going to talk about one of the most important aspects of Golang programming — functions and packages. Specifically, we’re going to learn how to write our own packages and make our code more modular and reusable.

So, let’s start with a quick review. In Golang, a function is a block of code that performs a specific task. We use functions to break down our code into smaller, more manageable pieces, which makes our code easier to read and debug.

Example 1: Simple function

func add(x int, y int) int {

return x + y

}

Here’s a simple example of a Golang function. As you can see, it takes two integer parameters and returns their sum.

So, now that we have a basic understanding of functions, let’s talk about packages. A package in Golang is a way to organize related functions, types, and variables into a single, reusable unit.

Example 2: Simple package

package rectangle

func Area(length float64, width float64) float64 {

return length * width

}

Here’s an example of a simple Golang package. As you can see, it contains a single function that calculates the area of a rectangle.

Now, let’s say we want to use this package in our main program. We can do that by importing the package into our code using the “import” keyword.

Example 3: Using the package in main program

package main

import (

"fmt"

"rectangle"

)

func main() {

length := 5.0

width := 10.0

area := rectangle.Area(length, width)

fmt.Printf("Area of rectangle with length %f and width %f = %f", length, width, area)

}

Here’s an example of how to import the “rectangle” package into our main program.

But what if we want to create our own package? Well, it’s actually quite easy! All we have to do is create a new folder with the name of our package and put our Golang files in that folder.

Example 4: Creating a new package

- mypackage

- functions.go

- types.go

Here’s an example of what our new package folder might look like. As you can see, it contains two Golang files — one with our functions and one with our types.

And that’s it! Now we can use our new package in our main program just like any other package.

Example 5: Using the new package in main program

package main

import (

"fmt"

"mypackage"

)

func main() {

myNumber := mypackage.Double(5)

myString := mypackage.Reverse("Hello, world!")

fmt.Printf("Double of 5 is %d\n", myNumber)

fmt.Printf("Reversed string is %s\n", myString)
}

Here’s an example of how to use our new package in our main program. As you can see, we simply import the package and then use its functions and types in our code.

So there you have it — a quick introduction to writing your own Golang packages. By using packages, we can make our code more modular, reusable, and easier to maintain.

Thanks for reading, and I’ll see you in the next tutorial!

--

--