OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / magiconair / properties / example_test.go
1 // Copyright 2017 Frank Schroeder. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package properties
6
7 import (
8         "fmt"
9         "log"
10 )
11
12 func ExampleLoad_iso88591() {
13         buf := []byte("key = ISO-8859-1 value with unicode literal \\u2318 and umlaut \xE4") // 0xE4 == ä
14         p, _ := Load(buf, ISO_8859_1)
15         v, ok := p.Get("key")
16         fmt.Println(ok)
17         fmt.Println(v)
18         // Output:
19         // true
20         // ISO-8859-1 value with unicode literal ⌘ and umlaut ä
21 }
22
23 func ExampleLoad_utf8() {
24         p, _ := Load([]byte("key = UTF-8 value with unicode character ⌘ and umlaut ä"), UTF8)
25         v, ok := p.Get("key")
26         fmt.Println(ok)
27         fmt.Println(v)
28         // Output:
29         // true
30         // UTF-8 value with unicode character ⌘ and umlaut ä
31 }
32
33 func ExampleProperties_GetBool() {
34         var input = `
35         key=1
36         key2=On
37         key3=YES
38         key4=true`
39         p, _ := Load([]byte(input), ISO_8859_1)
40         fmt.Println(p.GetBool("key", false))
41         fmt.Println(p.GetBool("key2", false))
42         fmt.Println(p.GetBool("key3", false))
43         fmt.Println(p.GetBool("key4", false))
44         fmt.Println(p.GetBool("keyX", false))
45         // Output:
46         // true
47         // true
48         // true
49         // true
50         // false
51 }
52
53 func ExampleProperties_GetString() {
54         p, _ := Load([]byte("key=value"), ISO_8859_1)
55         v := p.GetString("another key", "default value")
56         fmt.Println(v)
57         // Output:
58         // default value
59 }
60
61 func Example() {
62         // Decode some key/value pairs with expressions
63         p, err := Load([]byte("key=value\nkey2=${key}"), ISO_8859_1)
64         if err != nil {
65                 log.Fatal(err)
66         }
67
68         // Get a valid key
69         if v, ok := p.Get("key"); ok {
70                 fmt.Println(v)
71         }
72
73         // Get an invalid key
74         if _, ok := p.Get("does not exist"); !ok {
75                 fmt.Println("invalid key")
76         }
77
78         // Get a key with a default value
79         v := p.GetString("does not exist", "some value")
80         fmt.Println(v)
81
82         // Dump the expanded key/value pairs of the Properties
83         fmt.Println("Expanded key/value pairs")
84         fmt.Println(p)
85
86         // Output:
87         // value
88         // invalid key
89         // some value
90         // Expanded key/value pairs
91         // key = value
92         // key2 = value
93 }