Go Configure – Josh Betz https://josh.blog/2017/04/go-configure

Go Configure

APRIL 27, 2017 #

I’ve been dabbling in Golang on and off for a little while now. Recently I was looking for a package to read and parse configuration files with the following requirements in mind:

  1. Be fast.
  2. Use environment variables and JSON files (in that order).
  3. Support hot reloading.

I looked at some of the popular configuration packages, but didn’t see anything that I loved. Many of them don’t meet the requirements I had in mind or are more complex than I’d like.

So, I decided to write config — I know, it’s a great name. It’s pretty simple. We cache values in memory to minimize disk IO. On SIGHUP (or any time you call config.Reload()), we clear the cache, so you can update the configuration without restarting your app.

Usage looks something like this:

 

There are more examples and details on the API in the README on Github.

If you have questions or ideas, issues and pull requests are welcome on Github. Let me know if you use it!

package main

import (
"fmt"
"log"
"net/http" "github.com/joshbetz/config"
) func main() {
c := config.New("config.json") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var value string
err := c.Get("value", &value) if err != nil {
fmt.Fprintf(w, "Error: %s\n", err)
return
} fmt.Fprintf(w, "Value: %s\n", value)
}) log.Fatal(http.ListenAndServe(":3000", nil))
}

  

Go Configure Support hot reloading.的更多相关文章

  1. Webpack vs Browersify vs SystemJs for SPAs

    https://engineering.velocityapp.com/webpack-vs-browersify-vs-systemjs-for-spas-95b349a41fa0 Right no ...

  2. 构建通用的 React 和 Node 应用

    这是一篇非常优秀的 React 教程,这篇文章对 React 组件.React Router 以及 Node 做了很好的梳理.我是 9 月份读的该文章,当时跟着教程做了一遍,收获很大.但是由于时间原因 ...

  3. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q4-Q5)

    Question 4 You are designing a SharePoint 2010 application to store 50 GB of digital assets, includi ...

  4. Canu Parameter Reference(canu参数介绍)

    链接:Canu Parameter Reference To get the most up-to-date options, run canu -options The default values ...

  5. [React + webpack] hjs-webpack

    You can easily spend hours configuring the perfect dev environment with all the latest hotness like ...

  6. UE4读取脑电波MindWave插件(展示如何使用第三方库制作UE4插件)

    MyEEGPlugin.uplugin { , , "VersionName": "1.0", "FriendlyName": " ...

  7. UE4修改自Rama的UDP通信蓝图插件

    UE4.15没有提供蓝图UDP的组件,可以在网上找到一个ID叫Rama写的源代码,我把它封装成插件了(MyUdpPlugin),方便在各个UE4版本工程中使用UDP通信. 使用方式: 1.在自己的工程 ...

  8. (原)Unreal源码搬山-动画篇 自定义动画节点(一)

    @author:黑袍小道 太忙了,来更新下,嘿嘿 前言: 本文是接着上文 Unreal搬山之动画模块_Unreal动画流程和框架,进行简单入门如何自定义动画图标的AnimNode. 正文: 一.Ani ...

  9. uboot的readme

    ## (C) Copyright 2000 - 2008# Wolfgang Denk, DENX Software Engineering, wd@denx.de.## See file CREDI ...

随机推荐

  1. MySQL 插入数据时,中文乱码问题的解决

    当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1995-10-08','2015-11-12',' ...

  2. 【Java面试题】57 short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?

    Java规范有这样的规则 [ 1.高位转低位需要强制转换 2.低位转高位自动转. ] short s1 = 1; s1 = s1 + 1;有什么错? 答: i 是int 型   s1 short型 通 ...

  3. 【Java 线程的深入研究1】Java 提供了三种创建线程的方法

    Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...

  4. PHP生成UTF-8编码的CSV文件用Excel打开乱码的解决办法

    什么是BOM? 在UCS 编码中有一个叫做”ZERO WIDTH NO-BREAK SPACE”的字符,它的编码是FEFF.而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中.UCS规范建 ...

  5. Spring-Resource接口

    4.1.1 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源(JBoss AS 5.x上的VFS资源)等等很 ...

  6. Java精选笔记_面向对象(多态、异常)

    多态 概述 可以理解为事物存在的多种体现形态.同样的引用调用同样的方法却做了不同的事情 多态的本质是:一个程序中同名的不同方法. 多态的体现 父类的引用指向子类的对象,父类的引用接收子类的对象. 多态 ...

  7. 使用mt_rand代替rand

    mt_rand比rand生成的随机数“更随机”,重复值较少 下面是测试: <?php function t1($num=10000){ $arr = array(); for ($i=0; $i ...

  8. Python3 抓取豆瓣电影Top250

    利用 requests 抓取豆瓣电影 Top 250: import re import requests def main(url): global num headers = {"Use ...

  9. 【golang】 go语言之环境搭建+ 第一个go语言程序

    1 下载:http://www.golangtc.com/download (以win为例) 2 安装步骤: 安装包: go1.8.windows-amd64.msi 双击进行安装 创建工作目录 :D ...

  10. 如何搭建本地WordPress

    今天就来介绍一下如何在Windows下搭建本地WordPress. 安装前准备 1.正常的电脑 2.PHPNow  http://www.phpnow.org 这里面的PHPNow环境包其实包含了常见 ...