콘텐츠로 이동

설정 개요

다음 패키지를 사용합니다: 패키지 github.com/zeromicro/go-zero/core/conf conf 로 부하 it.

이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다.

두 번째 단계 goes 에서 로 prepare 설정 파일 based 에서 설정.

세 번째 party loading 설정 via conf.MustLoad

특정 Usage:

main.go

package main
import (
"flag"
"github.com/zeromicro/go-zero/core/conf"
)
type Config struct {
Host string `json:",default=0.0.0.0"`
Port int
}
var f = flag.String("f", "config.yaml", "config file")
func main() {
flag.Parse()
var c Config
conf.MustLoad(*f, &c)
println(c.Host)
}

설정.yaml

Host: 127.0.0.1
Port: 8888

이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다.

func Load(file string, v interface{}, opts ...Option) error

현재 지원하는 설정 format below:

  • json
  • yaml | yml
  • toml
  • json5 (since v1.10.1)

또한 로드하는 방법을 제공합니다 바이너리 데이터 에서 conf 패키지:

func LoadFromJsonBytes(content []byte, v interface{}) error
func LoadFromTomlBytes(content []byte, v interface{}) error
func LoadFromYamlBytes(content []byte, v interface{}) error
func LoadFromJson5Bytes(content []byte, v interface{}) error

간단한 예제:

text := []byte(`a: foo
B: bar`)
var val struct {
A string
B string
}
_ = LoadFromYamlBytes(text, &val)

이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다.

Host: "127.0.0.1"
host: "127.0.0.1"
var c struct {
Name string
}
conf.MustLoad("config.yaml", &c, conf.UseEnv())
Name: ${SERVER_NAME}
var c struct {
Name string `json:",env=SERVER_NAME"`
}
conf.MustLoad("config.yaml", &c)
type Config struct {
Name string // No 예시입니다
Port int64 `json:",default=8080"` // If 예시입니다
Path string `json:",optional"`
}

이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다.

Receive rules참고샘플
optional이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다.`json:“foo,optional”`
옵션Current 매개변수 can 만 receive enumeration valueProtestant 1:portrait line\
기본값Current Argument 기본값`json:“gender,default=male”`
range해당 항목의 동작과 사용법을 설명합니다.`json:“age,range=[0:120]“`
envCurrent 매개변수 are taken 에서 environmental variables`json:“mode,env=MODE”`

:::note Range expression rules

  1. 이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다. :::

More 참조 unmarshaler_test.go

이 항목은 해당 기능의 사용 방법, 설정, 주의 사항을 설명합니다.

type Config struct {
Etcd discov.EtcdConf
UserRpc zrpc.RpcClientConf
PortRpc zrpc.RpcClientConf
OtherRpc zrpc.RpcClientConf
}
const str = `
Etcd:
Key: rpcServer"
Hosts:
- "127.0.0.1:6379"
- "127.0.0.1:6377"
- "127.0.0.1:6376"
UserRpc:
Etcd:
Key: UserRpc
Hosts:
- "127.0.0.1:6379"
- "127.0.0.1:6377"
- "127.0.0.1:6376"
PortRpc:
Etcd:
Key: PortRpc
Hosts:
- "127.0.0.1:6379"
- "127.0.0.1:6377"
- "127.0.0.1:6376"
OtherRpc:
Etcd:
Key: OtherRpc
Hosts:
- "127.0.0.1:6379"
- "127.0.0.1:6377"
- "127.0.0.1:6376"
`

We must 추가 Hosts 로 모든 Etcd과 other base configurations.

// A, RpcClientConf 예시입니다
RpcClientConf struct {
Etcd discov.EtcdConf `json:",optional,inherit"`
....
}
const str = `
Etcd:
Key: rpcServer"
Hosts:
- "127.0.0.1:6379"
- "127.0.0.1:6377"
- "127.0.0.1:6376"
UserRpc:
Etcd:
Key: UserRpc
PortRpc:
Etcd:
Key: PortRpc
OtherRpc:
Etcd:
Key: OtherRpc
`

Key differences 에서 plain JSON:

FeatureJSONJSON5
주석✅ (///* */)
Trailing commas
Unquoted keys
Single-quoted strings
{
// Service 예시입니다
Host: "0.0.0.0",
Port: 8080, // 예시입니다
Name: "my-service",
}
var c Config
conf.MustLoad("config.json5", &c)
data := []byte(`{
// comment
Host: "localhost",
Port: 8080,
}`)
var c Config
conf.LoadFromJson5Bytes(data, &c)