Go Package (Structure) Design
Tags: go, godesign
Published: 2019-04-23 02:39:23

---
title: Go Package (Structure) Design
published: true
description: this post explains how to design the Go package
tags: Go, Go Design
---

![Package](https://images.unsplash.com/photo-1512418490979-92798cec1380?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80)<figcaption>Photo by Leone Venter on Unsplash</figcaption>

>Good package name tells you what it provides, not what it contains
>-- Practical Go, Dave Cheney

I do believe in User-Centric Design. It is not only about UX/UI. It is about how things will be used. So when I create functions, programs, APIs, I always think about how to use them. And this is why I love Test-Driven Development (TDD) methodology as well because it encourages me to think about how my function will be used (with tests).

---

The package is the unit in a Go program that other packages or modules use it. When I create a package, I'll ask how this package will be used.

## In Go, we always use functions through the package.

The package name is essential in Go. So this is the guideline,

>The package name must be unique in your Go program

If you have found that packages name have been used multiple time in your Go program, those packages would be too generic, or there is some design problem that needs to validate.

To demonstrate creating a Go package, think about shopping cart on e-commerce. I'm going to provide "add to cart" function. I've started with the cart package.

>When you create a Go package, always put Go file with the same name as a package name.

Every time I create a new package, I always put Go file with the same name as the package name. And I'll maintain all functionality of this package in this Go file until I understand the boundary of this package, then use another Go files to re-organize this package. If some feature doesn't make sense to be in this package, I'll move it to a new package.

{% gist https://gist.github.com/ballweera/bc4fda5f25b68097b50975e7b2308adb %}

From the code snippet, two entities need to consider; Cart and Product. Cart is the receiver of Add method, so I create Cart struct. And also generate Product struct. From this point, I create the "New" function that will return a Cart.

{% gist https://gist.github.com/ballweera/4e48f649306f84e895bbe4f7d61758a7 %}

When you want a cart, you can call New function.

>c := cart.New()

It is quite natural and readable in my point of view.

Think about creating a new product. You would have seen `pd := cart.NewProduct()`. As I mentioned, the package name will tell you what it provides. From my perspective, the cart should not be responsible for proving the product's functionality. So I decided to create a new package; product and move Product to a new one. When you want to create a product, you can call New function from the product package like `pd := product.New()`

{% gist https://gist.github.com/ballweera/c0ead46fa20fe57b7ea637a15f3bc001 %}

## Keep using Go file to re-organize your package, use sub-package when it makes sense

In the Go program, mostly, we don't call a package function with package chaining like this; `net.http.Get("http://www.example.com")`. So we need to keep the package name being unique in the Go program. We can create sub-package if it is reasonable. the net/http is an excellent example for sub-package.

So when you want to re-organize data in your package, you can use Go file to group related data and function and give the meaningful name to a file. Let's see this example

>common/kafka/producer
>common/kafka/consumer

From that design, when you want to create a producer or a consumer, you will use `pdc := producer.New()` and `csm := consumer.New()`

It seems to be OK. But it loses the context. What do the producer and consumer mean?

One thing I want to mention is

>Avoid package name like base, common, or util

Those packages are too generic. If there is something you would reuse, you will put them to those packages. And then the package name cannot tell you what it provides.

So I decide to refactor the above package to this

{% gist https://gist.github.com/ballweera/9a4d717e4760ca2e545f358d4a5d10fa %} 

Then you can create a producer and consumer with `pdc := kafka.NewProducer()` and `csm := kafka.NewConsumer()`

When you face that your package keeps growing, you can use Go file to re-organize data and functionality. From the previous example, I'll create producer.go and consumer.go to organizer data and functionalities of a producer and a consumer instead of creating sub-package.

{% gist https://gist.github.com/ballweera/b5564f5b60153d9c015767a776e105dc %}
{% gist https://gist.github.com/ballweera/ce3c157790020a0a3b152b7dcbb3edc5 %}

![Go program structure](https://cdn-images-1.medium.com/max/1600/1*UCYQJH_6v7fLx0VijyvRPw.png)

Most of the time, I don't design all packages of Go program in the first place. But I will think about how the package will be used and what it should provide.

I hope you will have fun to create software that improves our user's life gets better.