Golang Tips & Tricks #4 - internal folders

While developing a library, we create a directory structure to keep the code organized. However, some exported functions or struct should not be used by users of the library. The achieve that, call the package internal.

├── bar
│   ├── bar.go
│   └── internal
│       └── foobar.go
├── internal
│   └── foo.go
└── main.go

In the example above, the foo.go can be included only in the main.go. What’s more, only the bar.go is able to include the foobar.go file. It means, that only the direct parent of the internal package is allowed to use it’s internals.

Use this feature reasonably!

golang   tipstricks
Published in: Golang
Did you like the post?

About the author

Hi! My name is Bartek. I'm a developer since 2011. I love to learn by sharing the knowledge. You can find me on Twitter or Linkedin. I'm a backend engineer at Brainly.

Recent posts

I want to help you become a developer

That's why I can let you know about new blogposts on your email

You can find me

You May Also Enjoy

Golang Tips & Tricks #6 - the _test package 14 Aug 2019

Testing is one of the hardest stuff in programming. Today trick will help you organize your tests and the production code. Let's assume you have a package called `orders`. When you want to separat...
golang   tipstricks   tests

Golang Tips & Tricks #5 - blank identifier in structs 22 Jul 2019

While working with structures, there's a possibility to initialize the structure without providing the keys of fields. ```go type SomeSturct struct { FirstField string SecondField bool } // ....
golang   tipstricks

Golang Tips & Tricks #3 - graceful shutdown 18 Mar 2019

In the microservices’ world, one thing what’s worth considering is a graceful shutdown. This is important to not lose data while shutting down a container. The container orchestrator like Kubernete...
golang   tipstricks