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

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

  •  
  •   xmlHexagram · Mar 2, 2022 · 2614 views
    This topic created in 1710 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
    xmlHexagram
    OP
    Mar 2, 2022
    1
    主要构想是定义 struct 并且映射环境变量到 struct 上,像是 json.Unmarshal()那样。

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

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