Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 处理服务配置的最佳方式是什么?_Go_Configuration_Architecture_Config Files - Fatal编程技术网

Go 处理服务配置的最佳方式是什么?

Go 处理服务配置的最佳方式是什么?,go,configuration,architecture,config-files,Go,Configuration,Architecture,Config Files,我正在寻找在Go中读取大中型项目配置文件的最佳方法 哪个库适合读取和写入配置文件 我应该以什么格式保存配置文件(config.json或.env或config.yaml或…) 在Golang中处理配置的方法有很多。如果您想处理config.json,请查看。要处理环境变量,可以使用os包,如: // Set Environment Variable os.Setenv("FOO", "foo") // Get Environment Variable fo

我正在寻找在Go中读取大中型项目配置文件的最佳方法

  • 哪个库适合读取和写入配置文件
  • 我应该以什么格式保存配置文件(
    config.json
    .env
    config.yaml
    或…)

  • 在Golang中处理配置的方法有很多。如果您想处理config.json,请查看。要处理环境变量,可以使用
    os
    包,如:

    // Set Environment Variable
    os.Setenv("FOO", "foo")
    // Get Environment Variable
    foo := os.Getenv("FOO")
    // Unset Environment Variable
    os.Unsetenv("FOO")
    // Checking Environment Variable
    foo, ok := os.LookupEnv("FOO")
    if !ok {
      fmt.Println("FOO is not present")
    } else {
      fmt.Printf("FOO: %s\n", foo)
    }
    // Expand String Containing Environment Variable Using $var or ${var}
    fooString := os.ExpandEnv("foo${foo}or$foo") // "foofooorfoo"
    
    您还可以使用软件包:


    查看更多信息。

    这个问题似乎是基于观点的。你能确定你在寻找什么样的特质吗?对图书馆的要求是离题的。你的问题的其余部分也是完全基于意见的。在我看来,例如:从来没有任何理由使用.env文件。嗨!不幸的是,您的问题存在多个问题:1)这是因为它不涉及任何与编程相关的问题;2) 你实际上有很多问题;3) 您似乎一心想使用
    .env
    文件进行配置这并不坏,但我想说它们的用途不同于“常规”配置,并且不应该被服务本身阅读,而是应该被启动这些服务的任何人阅读……因此我建议尝试使用来问你的问题:
    r/golang
    subreddit或gopher的Slack频道或Golangbridge论坛都应该在不违反主题规则的情况下工作;因此,它不适合这种开放式/研究/教育式的问题。
    # .env file
    FOO=foo
    
    // main.go
    package main
    
    import (
      "fmt"
      "log"
      "os"
    
      "github.com/joho/godotenv"
    )
    
    func main() {
      // load .env file
      err := godotenv.Load(".env")
      if err != nil {
        log.Fatalf("Error loading .env file")
      }
      // Get Evironment Variable
      foo := os.Getenv("FOO")