Read .env files in GoLang using Viper

Maneesha Indrachapa
5 min readAug 14, 2022

You may be wondering why we need some library named Viper to read .env files in Go lang. Can’t we read them normally using a file writer or something? Let me answer that question. If you have a big project with many environments and varying configuration file types, you have to write a huge piece of code for that. Viper is there to save you from that trouble

What is the Viper?

According to the official GitHub documentation for the Viper,

Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats.

Here we are going to develop a simple program where we can read the .env files.

Let's write some code 💻👾

I hope you have installed Go on your computer and here I’m not going to talk about how to install Go. If you want to check please refer to this link

To create a Go project on your computer you can simply create a folder and go inside that folder and run the below commands in terminal

--

--