pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/example_test.go (about) 1 package knf 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 func ExampleGlobal() { 17 // Load global config 18 err := Global("/path/to/your/config.knf") 19 20 if err != nil { 21 fmt.Printf("Error: %v\n", err) 22 return 23 } 24 25 // Read string value 26 GetS("section:string") 27 28 // Read integer value 29 GetI("section:int") 30 31 // Read float value 32 GetF("section:float") 33 34 // Read boolean value 35 GetB("section:boolean") 36 37 // Read file mode value 38 GetM("section:file-mode") 39 40 // Read duration in seconds 41 GetD("section:duration") 42 43 // Check section 44 if HasSection("section") { 45 // Section exist 46 } 47 48 // Check property 49 if HasProp("section:string") { 50 // Property exist 51 } 52 53 // Slice of all sections 54 Sections() 55 56 // Slice of all properties in section 57 Props("section") 58 } 59 60 func ExampleReload() { 61 err := Global("/path/to/your/config.knf") 62 63 if err != nil { 64 fmt.Printf("Error: %v\n", err) 65 return 66 } 67 68 changes, err := Reload() 69 70 if err != nil { 71 fmt.Printf("Error: %v\n", err) 72 return 73 } 74 75 // Print info about changed values 76 for prop, changed := range changes { 77 fmt.Printf("Property %s changed → %t\n", prop, changed) 78 } 79 }