Categories: Go

Go – How to Create a Package in Go Language


This blog represents code samples and related concepts on how to create a package or library using GO programming language.

Create a HelloWorld Package

Save the following code within some file such as hello.go within some package folder such as helloworld within folder $GOPATH/src. Create the package using command such as go install helloworld. Alternatively, go inside the folder helloworld and execute the command, go install. This would lead to creation of *.a file within $GOPATH/pkg folder. Note that command such as go build can be used to compile and check if there are any errors.

package helloworld

func PrintHello(name string) string {
    helloWorldMessage := "Hello " + name + ", How are you doing today?"
    return helloWorldMessage
}

Pay attention to some of the following:

  • The methods to be exported MUST start with capital letters. Otherwise, trying to use the method would print the error message such as “cannot refer to unexported name”
  • Package folder name, file name and actual package name can all be different. The package *.a is created based on package folder name. However, the API or method needs to be called on the name used with package when defining the package in the GO program.

Create Another Package which uses HelloWorld Package

package main

import (
        "fmt"
        "helloworld"
)

func main() {
        fmt.Printf("---- Program to Demonstrate HelloWorld Package ----\n")
        helloMessage := helloworld.PrintHello("Ajitesh Shukla")
        fmt.Printf(helloMessage + "\n")
}

Save the above within another package (folder), say, hello with a fole such as hello.go. Install the package using command such as go install. This would create an executable with name such as hello within $GOPATH/bin folder. Execute the program using command such as $GOPATH/bin/hello. In case, you have set the $GOPATH/bin in PATH variable, execute the program by typing hello.


Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar
Tags: go

Recent Posts

Chunking Strategies for RAG with Examples

If you've built a "Naive" RAG pipeline, you've probably hit a wall. You've indexed your…

6 days ago

RAG Pipeline: 6 Steps for Creating Naive RAG App

If you're starting with large language models, you must have heard of RAG (Retrieval-Augmented Generation).…

7 days ago

Python: List Comprehension Explained with Examples

If you've spent any time with Python, you've likely heard the term "Pythonic." It refers…

1 week ago

Large Language Models (LLMs): Four Critical Modeling Stages

Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…

3 months ago

Agentic Workflow Design Patterns Explained with Examples

As Large Language Models (LLMs) evolve into autonomous agents, understanding agentic workflow design patterns has…

3 months ago

What is Data Strategy?

In today's data-driven business landscape, organizations are constantly seeking ways to harness the power of…

3 months ago