github.com/annwntech/go-micro/v2@v2.9.5/config/source/file/README.md (about) 1 # File Source 2 3 The file source reads config from a file. 4 5 It uses the File extension to determine the Format e.g `config.yaml` has the yaml format. 6 It does not make use of encoders or interpet the file data. If a file extension is not present 7 the source Format will default to the Encoder in options. 8 9 ## Example 10 11 A config file format in json 12 13 ```json 14 { 15 "hosts": { 16 "database": { 17 "address": "10.0.0.1", 18 "port": 3306 19 }, 20 "cache": { 21 "address": "10.0.0.2", 22 "port": 6379 23 } 24 } 25 } 26 ``` 27 28 ## New Source 29 30 Specify file source with path to file. Path is optional and will default to `config.json` 31 32 ```go 33 fileSource := file.NewSource( 34 file.WithPath("/tmp/config.json"), 35 ) 36 ``` 37 38 ## File Format 39 40 To load different file formats e.g yaml, toml, xml simply specify them with their extension 41 42 ``` 43 fileSource := file.NewSource( 44 file.WithPath("/tmp/config.yaml"), 45 ) 46 ``` 47 48 If you want to specify a file without extension, ensure you set the encoder to the same format 49 50 ``` 51 e := toml.NewEncoder() 52 53 fileSource := file.NewSource( 54 file.WithPath("/tmp/config"), 55 source.WithEncoder(e), 56 ) 57 ``` 58 59 ## Load Source 60 61 Load the source into config 62 63 ```go 64 // Create new config 65 conf := config.NewConfig() 66 67 // Load file source 68 conf.Load(fileSource) 69 ``` 70