Exemple #1
0
func Test_MarshalNotStruct(t *testing.T) {
	i := 0
	_, err := cfg.Marshal(i)
	if err == nil {
		t.Errorf("Did not get error when trying to marshal int\n")
	}
}
Exemple #2
0
func ExampleMarshal() {
	type MyConfig struct {
		Answer     int
		Pi         float64
		IsActive   bool   `cfg:"is_active"`
		Quotes     string `cfg:"quotes"`
		unexported string
		NotUsed    string `cfg:"-"`
	}

	var myConfig = &MyConfig{
		Answer:     42,
		Pi:         3.14,
		IsActive:   true,
		Quotes:     "Alea iacta est\nEt tu, Brute?",
		unexported: "foo",
		NotUsed:    "bar",
	}

	data, err := cfg.Marshal(myConfig)
	if err != nil {
		// Handle error
	}

	fmt.Printf("%s", data)
	// Output:
	// Answer = 42
	// Pi = 3.14
	// is_active = true
	// quotes = Alea iacta est\nEt tu, Brute?
}
Exemple #3
0
func Test_MarshalWithEmpty(t *testing.T) {
	data, err := cfg.Marshal(myConfigWithEmpty)
	if err != nil {
		t.Errorf("Error encoding data: %s\n", err)
	}

	if string(data) != myConfigEncodedWithEmpty {
		t.Errorf("Expected %q got %q\n", myConfigEncodedWithEmpty, string(data))
	}
}