The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
xmlHexagram

写了一个 golang 解析环境变量的工具库,准备用在生产里。

  •  
  •   xmlHexagram · Mar 2, 2022 · 2345 views
    This topic created in 1545 days ago, the information mentioned may be changed or developed.

    将环境变量映射到一个 struct 上,准备去给公司代码下毒。

    因为公司里项目不使用配置文件,都是通过环境变量,而 viper 的环境变量解析不是很方便,就自己写了一个。

    在个人项目里面用过了,觉得挺方便的,整理了一下开源了出来。

    帮忙看看能不能说服 CTO 让我下毒。

    项目地址

    https://github.com/XMLHexagram/emp

    5 replies    2022-03-02 23:28:31 +08:00
    xmlHexagram
        1
    xmlHexagram  
    OP
       Mar 2, 2022
    主要构想是定义 struct 并且映射环境变量到 struct 上,像是 json.Unmarshal()那样。

    提供了通过 tagName 自定义 name, prefix, ignore 。

    并且可以配置 allowEmpty, AutoPrefix, DirectDefualt, ParseStringToArrayAndSlice 等帮助更方便的解析
    sanggao
        2
    sanggao  
       Mar 2, 2022
    很牛了
    Hanggi
        3
    Hanggi  
       Mar 2, 2022
    viper 不香吗?
    yiplee
        4
    yiplee  
       Mar 2, 2022
    类似需求我之前用过 https://github.com/kelseyhightower/envconfig ,也挺好用的
    xmlHexagram
        5
    xmlHexagram  
    OP
       Mar 2, 2022
    @Hanggi viper 不能映射环境变量到 struct (至少我没有翻到用法)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5588 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 08:32 · PVG 16:32 · LAX 01:32 · JFK 04:32
    ♥ Do have faith in what you're doing.