首页 > 其他分享 >Viper —— configuration solution for Go

Viper —— configuration solution for Go

时间:2023-11-03 20:58:07浏览次数:30  
标签:err yaml sc Viper Go mapstructure configuration config

1. support several formats of configuration

config.yaml

name: 'bobby'
port: 12334

main.go to quick start 

package main

import (
    "fmt"
    "github.com/spf13/viper"
)

type ServerConfig struct { 
    ServiceName string `mapstructure:"name"`    // using mapstructure to support other forms of config & using mapstructure pkg
    Port        int    `mapstructure:"port"`
}

func main() {
    v := viper.New() // return a Viper
    v.SetConfigFile("viper_test/ch01/config.yaml")
    //v.SetConfigFile("config.yaml")
    if err := v.ReadInConfig(); err != nil {
        panic(err)
    }
    sc := ServerConfig{}
    if err := v.Unmarshal(&sc); err != nil {  // unserilaize to Object
        panic(err)
    }
    fmt.Println(sc)
    fmt.Println(v.Get("name"))
}

 

标签:err,yaml,sc,Viper,Go,mapstructure,configuration,config
From: https://www.cnblogs.com/sabertobih/p/17808484.html

相关文章

  • 无涯教程-MongoDB - 更新数据
    MongoDB的update()和save()方法用于将文档更新为集合,update()方法更新现有文档值,而save()方法将现有文档替换为save()方法中传递的文档。MongoDBUpdate()方法update()方法更新现有文档中的值。>db.COLLECTION_NAME.update(SELECTION_CRITERIA,UPDATED_DATA)考虑到mycol集......
  • 【django开发】知识经验总结共50页md文档。今日分享:django项目搭建
    Django的主要目的是简便、快速的开发数据库驱动的网站。它强调代码复用,多个组件可以很方便的以"插件"形式服务于整个框架,Django有许多功能强大的第三方插件,你甚至可以很方便的开发出自己的工具包。这使得Django具有很强的可扩展性。它还强调快速开发和DRY(DoNotRepeatYourself)原......
  • 无涯教程-MongoDB - 查询数据
    在本章中,无涯教程将学习如何从MongoDB集合中查询文档。find()方法要查询MongoDB集合中的数据,您需要使用MongoDB的find()方法。find()方法的基本语法如下->db.COLLECTION_NAME.find()find()方法将以非结构化方式显示所有文档。pretty()方法要以格式化的方式显示输出,可以......
  • Proj. Unknown: Deciding Differential Privacy of Online Algorithms with Multiple
    Paperhttps://arxiv.org/abs/2309.06615Abstract背景:自动机A被称作查分隐私自动机:当对某些D,对任何隐私预算ε>0,该自动机是Dε-differentiallyprivate(ADiPautomatonisaparametricautomatonwhosebehaviordependsontheprivacybudget......
  • gongshi
    证明:\(T(n,m,k)\rightarrowO(nmk)\)输入过程\(\qquad\)输入的时间复杂度为:\[\begin{aligned}T_1(n,m,k)=O(n\timesk)\end{aligned}\]dp过程:\(\qquad\)状态转移方程的时间复杂度为常数项,即\(O(1)\)\(\qquad\)该过程发生\(n\timesm\timesk\)次,则状态......
  • 无涯教程-MongoDB - 插入数据
    在本章中,无涯教程将学习如何在MongoDB集合中插入文档。要将数据插入MongoDB集合,您需要使用MongoDB的insert()或save()方法。Insert-语法insert()命令的基本语法如下:>db.COLLECTION_NAME.insert(document)Insert-示例1>db.mycol.insert({_id:ObjectId(7df78ad8......
  • 无涯教程-MongoDB - 删除集合
    在本章中,无涯教程将看到如何使用MongoDB删除集合。MongoDB的db.collection.drop()用于从数据库中删除集合。DropCollection-语法drop()命令的基本语法如下-db.COLLECTION_NAME.drop()DropCollection-示例首先,将可用集合检入数据库mydb。>usemydbswitchedtod......
  • logstash-output-mongodb安装
     1.安装插件:[root@localhostbin]#./logstash-plugininstalllogstash-output-mongodbValidatinglogstash-output-mongodbInstallinglogstash-output-mongodb 2.查看插件版本./logstash-pluginlist--verboselogstash-output-mongodb(3.1.7) 同步报错误:[2023-11......
  • Princeton Algorithms, Part I week2 stack&queue
    stack:先进后出queue:先进先出首先是stack有两种实现方式,第一种是用链表,第二种是用数组。Stack:linked-listrepresentation   stack:arrayimplementation  上面这个实现是固定长度的arrayimplementation非常不方便,所以引入可变长度的实现resizing-array......
  • Go语言定时器实战:性能优化与实用技巧
    Go语言定时器实战:性能优化与实用技巧原创 Go先锋 Go先锋 2023-11-0307:58 发表于广东收录于合集#Go语言包29个Go先锋读完需要8分钟速读仅需3分钟  概述在日常开发中,定时器是一个非常常用且重要的功能。它可以让程序在特定的时间间隔内执行某些任务,比......