Monday, 23 December 2024

GO LANG environment management TOML FILE && VIPER && Unmarshal with Struct

 Go, the viper.Unmarshal function from the Viper library is used to map configuration data into a Go struct. This allows for structured access to configuration parameters, enhancing type safety and code readability.

Usage Example:

go

import ( "github.com/spf13/viper" "log" ) type Config struct { Port int Name string PathMap string `mapstructure:"path_map"` } func main() { // Set the file name of the configurations file viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name viper.AddConfigPath(".") // optionally look for config in the working directory // Find and read the config file if err := viper.ReadInConfig(); err != nil { log.Fatalf("Error reading config file: %v", err) } var config Config if err := viper.Unmarshal(&config); err != nil { log.Fatalf("Unable to decode into struct: %v", err) } // Use the config struct... }

Key Points:

  • Struct Tags: When the configuration file's keys differ from the struct's field names, use struct tags to map them appropriately. In the example above, the PathMap field corresponds to the path_map key in the configuration file, facilitated by the mapstructure tag.

  • Supported Formats: Viper supports various configuration file formats, including JSON, TOML, YAML, HCL, INI, envfile, and Java properties. Ensure that the correct format is specified using viper.SetConfigType() if the file extension is not present.

  • Error Handling: It's crucial to handle errors that may arise during the reading and unmarshalling of the configuration to prevent runtime panics.

For more detailed information and advanced usage, refer to the official Viper documentation.

1. YAML Configuration File (config.yaml)

yaml
port: 8080 name: "MyApp" path_map: "/usr/local/app"

2. JSON Configuration File (config.json)

json
{ "port": 8080, "name": "MyApp", "path_map": "/usr/local/app" }

3. TOML Configuration File (config.toml)

toml
port = 8080 name = "MyApp" path_map = "/usr/local/app"

4. INI Configuration File (config.ini)

ini
[default] port = 8080 name = MyApp path_map = /usr/local/app

5. Environment Variable Configuration

You can also use environment variables by setting them directly in your environment. Viper can read these values if configured properly.

bash
export MYAPP_PORT=8080 export MYAPP_NAME="MyApp" export MYAPP_PATH_MAP="/usr/local/app"

Reading the Configuration

If you're using any of the above configuration formats (YAML, JSON, TOML, etc.), you would load it with Viper like this:

go
import ( "github.com/spf13/viper" "log" ) type Config struct { Port int `mapstructure:"port"` Name string `mapstructure:"name"` PathMap string `mapstructure:"path_map"` } func main() { viper.SetConfigName("config") // name of the config file (no extension) viper.SetConfigType("yaml") // specify the format (could be json, toml, etc.) viper.AddConfigPath(".") // look for the config in the current directory if err := viper.ReadInConfig(); err != nil { log.Fatalf("Error reading config file: %v", err) } var config Config if err := viper.Unmarshal(&config); err != nil { log.Fatalf("Unable to decode into struct: %v", err) } // Now you can use config.Port, config.Name, and config.PathMap }

Explanation:

  • YAML: Common for configuration files due to its readability.
  • JSON: Widely used in APIs and for data serialization.
  • TOML: A newer format often used in configuration files.
  • INI: A simple and older format often used for legacy applications.
  • Environment Variables: Useful for deployment in containers or cloud environments.


The viper.AddConfigPath() method in Go can accept both relative and absolute paths. However, the behavior depends on how you specify the path.

1. Relative Path:

When you provide a relative path, Viper will resolve it based on the current working directory of the Go program (i.e., the directory from which the program is executed).

go

viper.AddConfigPath(".") // Relative path, current directory

This means Viper will look for the configuration file in the directory where your program is running.

2. Absolute Path:

When you provide an absolute path, Viper will use that exact path on the file system to locate the configuration file.

go

viper.AddConfigPath("/etc/myapp") // Absolute path


When you specify viper.AddConfigPath("config"), Viper will look for the configuration file in a relative directory named config in the current working directory where the Go program is being executed.

Explanation:

  • config in this case is treated as a relative path.
  • The "current working directory" is the directory from which you run the Go application (i.e., where your executable or script is being executed).

For example, if you run the Go program from the following directory:

bash
/home/user/myapp/

Viper will search for the config directory inside /home/user/myapp/ like this:

bash
/home/user/myapp/config/

If the program is executed from a different directory, Viper will look for the config directory relative to that directory instead.

Example Directory Structure:

If your project structure is:

arduino
/myapp /config config.yaml main.go

No comments:

Post a Comment