import ( "fmt" "github.com/spf13/viper" ) func main() { viper.SetConfigFile("config.yaml") // file name viper.ReadInConfig() fmt.Println(viper.Get("db.host")) // extracts data from config.yaml file }
import ( "fmt" "github.com/spf13/viper" ) func main() { viper.SetDefault("db.host", "localhost") viper.SetDefault("db.port", "3306") viper.SetDefault("db.username", "root") viper.SetDefault("db.password", "") fmt.Println(viper.Get("db.host")) // prints "localhost" }In conclusion, the "github.com/spf13/viper" package library is perfect for creating a centralized configuration storage unit for a project. You can choose to read and write configurations from a variety of sources, including files, environmental variables, and other configuration stores.