예제 #1
0
// ExampleDev shows how to use the config package.
func ExampleDev() {
	// Set up some basic environment variables.
	os.Setenv("DOCK_IP", "40.23.233.10")
	os.Setenv("DOCK_PORT", "4044")
	os.Setenv("DOCK_InitStamp", time.Date(2009, time.November,
		10, 15, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate))

	// Init() must be called only once with the given namespace to load.
	cfg.Init("dock")

	// NOTE: All keys must be in lowercase.

	// To get the ip.
	fmt.Println(cfg.String("ip"))

	// To get the port number.
	fmt.Println(cfg.Int("port"))

	// To get the timestamp.
	fmt.Println(cfg.Time("initstamp"))

	// Output:
	// 40.23.233.10
	// 4044
	// 2009-11-10 15:00:00 +0000 UTC
}
예제 #2
0
// TestNotExists validates the ability to load configuration values
// using the OS-level environment variables and panic when something
// is missing.
func TestNotExists(t *testing.T) {
	t.Log("Given the need to panic when environment variables are missing.")
	{
		os.Setenv("MYAPP_PROC_ID", "322")
		os.Setenv("MYAPP_SOCKET", "./tmp/sockets.po")
		os.Setenv("MYAPP_PORT", "4034")

		cfg.Init("myapp")

		t.Log("\tWhen given a namspace key to search for that does NOT exist.")
		{
			shouldPanic(t, "stamp", func() {
				cfg.Time("stamp")
			})

			shouldPanic(t, "pid", func() {
				cfg.Int("pid")
			})

			shouldPanic(t, "dest", func() {
				cfg.String("dest")
			})

		}
	}
}