當前位置: 首頁>>代碼示例>>Golang>>正文


Golang envconfig.Init函數代碼示例

本文整理匯總了Golang中github.com/vrischmann/envconfig.Init函數的典型用法代碼示例。如果您正苦於以下問題:Golang Init函數的具體用法?Golang Init怎麽用?Golang Init使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Init函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestParseSimpleConfig

func TestParseSimpleConfig(t *testing.T) {
	var conf struct {
		Name string
		Log  struct {
			Path string
		}
	}

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: keys NAME, name not found", err.Error())

	os.Setenv("NAME", "foobar")
	err = envconfig.Init(&conf)
	require.Equal(t, "envconfig: keys LOG_PATH, log_path not found", err.Error())

	os.Setenv("LOG_PATH", "/var/log/foobar")
	err = envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, "foobar", conf.Name)
	require.Equal(t, "/var/log/foobar", conf.Log.Path)

	// Clean up at the end of the test - some tests share the same key and we don't values to be seen by those tests
	os.Setenv("NAME", "")
	os.Setenv("LOG_PATH", "")
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:26,代碼來源:envconfig_test.go

示例2: TestParseWrongValues

func TestParseWrongValues(t *testing.T) {
	var conf struct{ OK bool }
	os.Setenv("OK", "foobar")
	err := envconfig.Init(&conf)
	require.Equal(t, `strconv.ParseBool: parsing "foobar": invalid syntax`, err.Error())

	var conf2 struct{ Port int }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf2)
	require.Equal(t, `strconv.ParseInt: parsing "foobar": invalid syntax`, err.Error())

	var conf3 struct{ Port uint }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf3)
	require.Equal(t, `strconv.ParseUint: parsing "foobar": invalid syntax`, err.Error())

	var conf4 struct{ Port float32 }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf4)
	require.Equal(t, `strconv.ParseFloat: parsing "foobar": invalid syntax`, err.Error())

	var conf5 struct{ Data []byte }
	os.Setenv("DATA", "foobar")
	err = envconfig.Init(&conf5)
	require.Equal(t, "illegal base64 data at input byte 4", err.Error())
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:26,代碼來源:envconfig_test.go

示例3: TestParseDefaultVal

func TestParseDefaultVal(t *testing.T) {
	var conf struct {
		MySQL struct {
			Master struct {
				Address string `envconfig:"default=localhost"`
				Port    int    `envconfig:"default=3306"`
			}
			Timeout      time.Duration `envconfig:"default=1m,myTimeout"`
			LocalTimeout time.Duration `envconfig:"myTimeout2,default=1m"`
		}
	}

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*1, conf.MySQL.Timeout)

	os.Setenv("myTimeout", "2m")
	os.Setenv("myTimeout2", "20m")

	err = envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*2, conf.MySQL.Timeout)
	require.Equal(t, time.Minute*20, conf.MySQL.LocalTimeout)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:28,代碼來源:envconfig_test.go

示例4: TestParseSliceConfig

func TestParseSliceConfig(t *testing.T) {
	var conf struct {
		Names  []string
		Ports  []int
		Shards []struct {
			Name string
			Addr string
		}
	}

	os.Setenv("NAMES", "foobar,barbaz")
	os.Setenv("PORTS", "900,100")
	os.Setenv("SHARDS", "{foobar,localhost:2929},{barbaz,localhost:2828}")

	err := envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, 2, len(conf.Names))
	require.Equal(t, "foobar", conf.Names[0])
	require.Equal(t, "barbaz", conf.Names[1])
	require.Equal(t, 2, len(conf.Ports))
	require.Equal(t, 900, conf.Ports[0])
	require.Equal(t, 100, conf.Ports[1])
	require.Equal(t, 2, len(conf.Shards))
	require.Equal(t, "foobar", conf.Shards[0].Name)
	require.Equal(t, "localhost:2929", conf.Shards[0].Addr)
	require.Equal(t, "barbaz", conf.Shards[1].Name)
	require.Equal(t, "localhost:2828", conf.Shards[1].Addr)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:29,代碼來源:envconfig_test.go

示例5: TestParseOptionalConfig

func TestParseOptionalConfig(t *testing.T) {
	var conf struct {
		Name    string        `envconfig:"optional"`
		Flag    bool          `envconfig:"optional"`
		Timeout time.Duration `envconfig:"optional"`
		Port    int           `envconfig:"optional"`
		Port2   uint          `envconfig:"optional"`
		Delta   float32       `envconfig:"optional"`
		DeltaV  float64       `envconfig:"optional"`
		Slice   []string      `envconfig:"optional"`
		Struct  struct {
			A string
			B int
		} `envconfig:"optional"`
	}

	os.Setenv("NAME", "")
	os.Setenv("FLAG", "")
	os.Setenv("TIMEOUT", "")
	os.Setenv("PORT", "")
	os.Setenv("PORT2", "")
	os.Setenv("DELTA", "")
	os.Setenv("DELTAV", "")
	os.Setenv("SLICE", "")
	os.Setenv("STRUCT", "")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "", conf.Name)
}
開發者ID:remeh,項目名稱:envconfig,代碼行數:30,代碼來源:envconfig_test.go

示例6: TestInvalidDurationConfig

func TestInvalidDurationConfig(t *testing.T) {
	var conf struct {
		Timeout time.Duration
	}

	os.Setenv("TIMEOUT", "foo")

	err := envconfig.Init(&conf)
	require.NotNil(t, err)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:10,代碼來源:envconfig_test.go

示例7: TestInvalidSliceElementValueKind

func TestInvalidSliceElementValueKind(t *testing.T) {
	var conf struct {
		Foo []interface{}
	}

	os.Setenv("FOO", "lalala")

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: kind interface not supported", err.Error())
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:10,代碼來源:envconfig_test.go

示例8: initializeConfig

// initialize config and turns it to map
func initializeConfig() map[string]string {
	err := envconfig.Init(&conf)
	if err != nil {
		log.Panic("Error on env config initialize! ", err.Error())
	}

	return map[string]string{
		"dbConnection": conf.DB.Connection,
	}
}
開發者ID:NikitaSmall,項目名稱:radio-go,代碼行數:11,代碼來源:config.go

示例9: TestUnmarshaler

func TestUnmarshaler(t *testing.T) {
	var conf struct {
		LogMode logMode
	}

	os.Setenv("LOGMODE", "file")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, logFile, conf.LogMode)

	var conf2 struct {
		LogMode *logMode
	}

	err = envconfig.Init(&conf2)
	require.Nil(t, err)
	require.Equal(t, logFile, *conf2.LogMode)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:19,代碼來源:envconfig_test.go

示例10: ExampleInit

func ExampleInit() {
	var conf struct {
		MySQL struct {
			Host     string
			Port     int
			Database struct {
				User     string
				Password string
				Name     string
			}
			Params struct {
				Charset string `envconfig:"-"`
			}
		}
		Log struct {
			Path   string `envconfig:"default=/var/log/mylog.log"`
			Rotate bool   `envconfig:"logRotate"`
		}
		NbWorkers int
		Timeout   time.Duration
		Cassandra struct {
			SSLCert string
			SSLKey  string
		}
	}

	os.Setenv("MYSQL_HOST", "localhost")
	os.Setenv("MYSQL_PORT", "3306")
	os.Setenv("MYSQL_DATABASE_USER", "root")
	os.Setenv("MYSQL_DATABASE_PASSWORD", "foobar")
	os.Setenv("MYSQL_DATABASE_NAME", "default")
	os.Setenv("logRotate", "true")
	os.Setenv("NBWORKERS", "10")
	os.Setenv("TIMEOUT", "120s")
	os.Setenv("CASSANDRA_SSL_CERT", "/etc/cassandra/ssl.crt")
	os.Setenv("CASSANDRA_SSL_KEY", "/etc/cassandra/ssl.key")

	if err := envconfig.Init(&conf); err != nil {
		fmt.Printf("err=%s\n", err)
	}

	fmt.Println(conf.MySQL.Database.User)
	fmt.Println(conf.Log.Rotate)
	fmt.Println(conf.Timeout)
	fmt.Println(conf.Log.Path)
	fmt.Println(conf.Cassandra.SSLCert)
	fmt.Println(conf.Cassandra.SSLKey)
	// Output:
	// root
	// true
	// 2m0s
	// /var/log/mylog.log
	// /etc/cassandra/ssl.crt
	// /etc/cassandra/ssl.key
}
開發者ID:remeh,項目名稱:envconfig,代碼行數:55,代碼來源:example_test.go

示例11: TestParseEmptyTag

func TestParseEmptyTag(t *testing.T) {
	var conf struct {
		Name string `envconfig:""`
	}

	os.Setenv("NAME", "foobar")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "foobar", conf.Name)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:11,代碼來源:envconfig_test.go

示例12: TestParseMapType

func TestParseMapType(t *testing.T) {
	var conf struct {
		Map myMapType
	}

	os.Setenv("MAP", "a")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, 1, conf.Map["a"])
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:11,代碼來源:envconfig_test.go

示例13: TestParseCustomNameConfig

func TestParseCustomNameConfig(t *testing.T) {
	var conf struct {
		Name string `envconfig:"customName"`
	}

	os.Setenv("customName", "foobar")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "foobar", conf.Name)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:11,代碼來源:envconfig_test.go

示例14: TestParseSkippableConfig

func TestParseSkippableConfig(t *testing.T) {
	var conf struct {
		Flag bool `envconfig:"-"`
	}

	os.Setenv("FLAG", "true")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, false, conf.Flag)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:11,代碼來源:envconfig_test.go

示例15: TestParseBoolConfig

func TestParseBoolConfig(t *testing.T) {
	var conf struct {
		DoIt bool
	}

	os.Setenv("DOIT", "true")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, true, conf.DoIt)
}
開發者ID:mozilla-services,項目名稱:go-syncstorage,代碼行數:11,代碼來源:envconfig_test.go


注:本文中的github.com/vrischmann/envconfig.Init函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。