Documentation
¶
Overview ¶
A golang implemented library to read/write .ini format files.
With this library, you can load the .ini file a string, a byte array, a file and a io.Reader.
import ( ini "github.com/ochinchina/go-ini" ) func main() { //load from .ini file ini := ini.Load( "myfile.ini") //load from .ini format string str_data := "[section1]\nkey1=value1\n[section2]\nkey2=value2" ini = ini.Load( str_data ) //load .ini format byte array ini = ini.Load( []byte(str_data) ) //load from io.Reader var reader io.Reader = ... ini = ini.Load( reader ) //load from multiple source in one Load method ini = ini.Load( "myfile.ini", reader, str_data, bytes_data ) }
The loaded Ini includes sections, you can access section:
//get all the sections in the .ini var sections []*Section = ini.Sections() //get a section by Name var section *Section = ini.GetSection( sectionName )
Then the key in a section can be accessed by method GetXXX() and GetXXXWithDefault(defValue):
//get the value of key value, err := section.GetValue( "key1") value = section.GetValueWithDefault("key1", "") //get value of key as int i, err := section.GetInt( "key2" ) i = section.GetIntWithDefault( "key2" )
Index ¶
- func SetDefaultSectionName(defSectionName string)
- type Ini
- func (ini *Ini) AddSection(section *Section)
- func (ini *Ini) GetBool(sectionName, key string) (bool, error)
- func (ini *Ini) GetBoolWithDefault(sectionName, key string, defValue bool) bool
- func (ini *Ini) GetDefaultSectionName() string
- func (ini *Ini) GetFloat32(sectionName, key string) (float32, error)
- func (ini *Ini) GetFloat32WithDefault(sectionName, key string, defValue float32) float32
- func (ini *Ini) GetFloat64(sectionName, key string) (float64, error)
- func (ini *Ini) GetFloat64WithDefault(sectionName, key string, defValue float64) float64
- func (ini *Ini) GetInt(sectionName, key string) (int, error)
- func (ini *Ini) GetInt64(sectionName, key string) (int64, error)
- func (ini *Ini) GetInt64WithDefault(sectionName, key string, defValue int64) int64
- func (ini *Ini) GetIntWithDefault(sectionName, key string, defValue int) int
- func (ini *Ini) GetSection(name string) (*Section, error)
- func (ini *Ini) GetUint(sectionName, key string) (uint, error)
- func (ini *Ini) GetUint64(sectionName, key string) (uint64, error)
- func (ini *Ini) GetUint64WithDefault(sectionName, key string, defValue uint64) uint64
- func (ini *Ini) GetUintWithDefault(sectionName, key string, defValue uint) uint
- func (ini *Ini) GetValue(sectionName, key string) (string, error)
- func (ini *Ini) GetValueWithDefault(sectionName, key string, defValue string) string
- func (ini *Ini) HasKey(sectionName, key string) bool
- func (ini *Ini) HasSection(name string) bool
- func (ini *Ini) Load(sources ...interface{})
- func (ini *Ini) LoadBytes(content []byte)
- func (ini *Ini) LoadFile(fileName string)
- func (ini *Ini) LoadReader(reader io.Reader)
- func (ini *Ini) LoadString(content string)
- func (ini *Ini) NewSection(name string) *Section
- func (ini *Ini) Sections() []*Section
- func (ini *Ini) SetDefaultSectionName(defSectionName string)
- func (ini *Ini) String() string
- func (ini *Ini) Write(writer io.Writer) error
- func (ini *Ini) WriteToFile(fileName string) error
- type Key
- type Properties
- func (p *Properties) GetBool(key string) (bool, error)
- func (p *Properties) GetBoolWithDefault(key string, defValue bool) bool
- func (p *Properties) GetFloat32(key string) (float32, error)
- func (p *Properties) GetFloat32WithDefault(key string, defValue float32) float32
- func (p *Properties) GetFloat64(key string) (float64, error)
- func (p *Properties) GetFloat64WithDefault(key string, defValue float64) float64
- func (p *Properties) GetInt(key string) (int, error)
- func (p *Properties) GetInt64(key string) (int64, error)
- func (p *Properties) GetInt64WithDefault(key string, defValue int64) int64
- func (p *Properties) GetIntWithDefault(key string, defValue int) int
- func (p *Properties) GetProperty(key string) (string, error)
- func (p *Properties) GetPropertyWithDefault(key string, defValue string) string
- func (p *Properties) GetUint(key string) (uint, error)
- func (p *Properties) GetUint64(key string) (uint64, error)
- func (p *Properties) GetUint64WithDefault(key string, defValue uint64) uint64
- func (p *Properties) GetUintWithDefault(key string, defValue uint) uint
- func (p *Properties) Load(sources ...interface{})
- type Section
- func (section *Section) Add(key, value string)
- func (section *Section) GetBool(key string) (bool, error)
- func (section *Section) GetBoolWithDefault(key string, defValue bool) bool
- func (section *Section) GetFloat32(key string) (float32, error)
- func (section *Section) GetFloat32WithDefault(key string, defValue float32) float32
- func (section *Section) GetFloat64(key string) (float64, error)
- func (section *Section) GetFloat64WithDefault(key string, defValue float64) float64
- func (section *Section) GetInt(key string) (int, error)
- func (section *Section) GetInt64(key string) (int64, error)
- func (section *Section) GetInt64WithDefault(key string, defValue int64) int64
- func (section *Section) GetIntWithDefault(key string, defValue int) int
- func (section *Section) GetUint(key string) (uint, error)
- func (section *Section) GetUint64(key string) (uint64, error)
- func (section *Section) GetUint64WithDefault(key string, defValue uint64) uint64
- func (section *Section) GetUintWithDefault(key string, defValue uint) uint
- func (section *Section) GetValue(key string) (string, error)
- func (section *Section) GetValueWithDefault(key string, defValue string) string
- func (section *Section) HasKey(key string) bool
- func (section *Section) Key(key string) Key
- func (section *Section) Keys() []Key
- func (section *Section) String() string
- func (section *Section) Write(writer io.Writer) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaultSectionName ¶
func SetDefaultSectionName(defSectionName string)
Types ¶
type Ini ¶
type Ini struct {
// contains filtered or unexported fields
}
manage all the sections and their key values defined in the .ini file
func Load ¶
func Load(sources ...interface{}) *Ini
Load the .ini from one of following resource:
- file
- string in .ini format
- byte array in .ini format
- io.Reader a reader to load .ini content
One or more source can be provided in this Load method, such as:
var reader1 io.Reader = ... var reader2 io.Reader = ... ini.Load( "./my.ini", "[section]\nkey=1", "./my2.ini", reader1, reader2 )
func (*Ini) AddSection ¶
add a section to the .ini file and overwrite the exist section with same name
func (*Ini) GetBool ¶
get the value of key in section as bool. return true if the value of the key is one of following(case insensitive):
- true
- yes
- t
- y
- 1
return false for all other values
func (*Ini) GetBoolWithDefault ¶
get the value of key as bool and return the default value if the section in the .ini file or key in the section does not exist
func (*Ini) GetDefaultSectionName ¶
func (*Ini) GetFloat32 ¶
get the value of key in the section as float32
func (*Ini) GetFloat32WithDefault ¶
get the value of key in the section as float32 and return defValue if the section in the .ini file or key in the section does not exist
func (*Ini) GetFloat64 ¶
get the value of key in the section as float64
func (*Ini) GetFloat64WithDefault ¶
get the value of key in the section as float64 and return defValue if the section in the .ini file or key in the section does not exist
func (*Ini) GetInt64WithDefault ¶
get the value of key in the section as int64 and return defValue if the section in the .ini file or key in the section does not exist
func (*Ini) GetIntWithDefault ¶
get the value of key in the section as int and return defValue if the section in the .ini file or key in the section does not exist
func (*Ini) GetUint64WithDefault ¶
get the value of key in the section as uint64 and return defValue if the section in the .ini file or key in the section does not exist
func (*Ini) GetUintWithDefault ¶
get the value of key in the section as int and return defValue if the section in the .ini file or key in the section does not exist
func (*Ini) GetValueWithDefault ¶
get the value of the key in section if the key does not exist, return the defValue
func (*Ini) HasKey ¶
check if a key exists or not in the Ini
return true if the key in section exists
func (*Ini) HasSection ¶
return true if the section with name exists return false if the section with name does not exist
func (*Ini) Load ¶
func (ini *Ini) Load(sources ...interface{})
Load from the sources, the source can be one of:
- fileName
- a string includes .ini
- io.Reader the reader to load the .ini contents
- byte array incldues .ini content
func (*Ini) LoadReader ¶
Explicitly loads .ini from a reader
func (*Ini) LoadString ¶
load ini from the content which contains the .ini formated string
func (*Ini) NewSection ¶
create a new section if the section with name does not exist or return the exist one if the section with name already exists
func (*Ini) SetDefaultSectionName ¶
func (*Ini) Write ¶
write the content of the .ini in the .ini file format, e.g. in following format:
[section1] key1 = value1 key2 = value2 [section2] key3 = value3 key4 = value4
func (*Ini) WriteToFile ¶
Write the conents of ini to a file
type Key ¶
type Key interface { // get name of the key Name() string // get value of the key Value() (string, error) //get the value of key and return defValue if //the value does not exist ValueWithDefault(defValue string) string // get the value as bool // return true if the value is one of following(case insensitive): // - true // - yes // - T // - Y // - 1 // Any other value will return false Bool() (bool, error) // get the value as bool and return the defValue if the // value of the key does not exist BoolWithDefault(defValue bool) bool // get the value as int Int() (int, error) // get value as int and return defValue if the // value of the key does not exist IntWithDefault(defValue int) int //get value as uint Uint() (uint, error) //get value as uint and return defValue if the //key does not exist or it is not uint format UintWithDefault(defValue uint) uint // get the value as int64 Int64() (int64, error) // get the value as int64 and return defValue // if the value of the key does not exist Int64WithDefault(defValue int64) int64 // get the value as uint64 Uint64() (uint64, error) // get the value as uint64 and return defValue // if the value of the key does not exist Uint64WithDefault(defValue uint64) uint64 // get the value as float32 Float32() (float32, error) // get the value as float32 and return defValue // if the value of the key does not exist Float32WithDefault(defValue float32) float32 // get the value as float64 Float64() (float64, error) // get the value as the float64 and return defValue // if the value of the key does not exist Float64WithDefault(defValue float64) float64 // return a string as "key=value" format // and if no value return empty string String() string }
represents the <key, value> pair stored in the section of the .ini file
type Properties ¶
type Properties struct {
// contains filtered or unexported fields
}
func NewProperties ¶
func NewProperties() *Properties
func (*Properties) GetBoolWithDefault ¶
func (p *Properties) GetBoolWithDefault(key string, defValue bool) bool
func (*Properties) GetFloat32 ¶
func (p *Properties) GetFloat32(key string) (float32, error)
func (*Properties) GetFloat32WithDefault ¶
func (p *Properties) GetFloat32WithDefault(key string, defValue float32) float32
func (*Properties) GetFloat64 ¶
func (p *Properties) GetFloat64(key string) (float64, error)
func (*Properties) GetFloat64WithDefault ¶
func (p *Properties) GetFloat64WithDefault(key string, defValue float64) float64
func (*Properties) GetInt64WithDefault ¶
func (p *Properties) GetInt64WithDefault(key string, defValue int64) int64
func (*Properties) GetIntWithDefault ¶
func (p *Properties) GetIntWithDefault(key string, defValue int) int
func (*Properties) GetProperty ¶
func (p *Properties) GetProperty(key string) (string, error)
func (*Properties) GetPropertyWithDefault ¶
func (p *Properties) GetPropertyWithDefault(key string, defValue string) string
func (*Properties) GetUint64WithDefault ¶
func (p *Properties) GetUint64WithDefault(key string, defValue uint64) uint64
func (*Properties) GetUintWithDefault ¶
func (p *Properties) GetUintWithDefault(key string, defValue uint) uint
func (*Properties) Load ¶
func (p *Properties) Load(sources ...interface{})
type Section ¶
type Section struct { //Name of the section Name string // contains filtered or unexported fields }
manages all the key/value defined in the .ini file format
func (*Section) GetBool ¶
Get the value of key as bool, it will return true if the value of the key is one of following( case insensitive):
- true
- yes
- t
- y
- 1
func (*Section) GetBoolWithDefault ¶
Get the value of key as bool and if the key does not exist, return the default value
func (*Section) GetFloat32 ¶
Get the value of the key as float32
func (*Section) GetFloat32WithDefault ¶
Get the value of the key as float32 and if the key does not exist return the default value
func (*Section) GetFloat64 ¶
Get the value of the key as float64
func (*Section) GetFloat64WithDefault ¶
Get the value of the key as float64 and if the key does not exist return the default value
func (*Section) GetInt64WithDefault ¶
Get the value of the key as int64 and if the key does not exist return the default value
func (*Section) GetIntWithDefault ¶
Get the value of the key as int and if the key does not exist return the default value
func (*Section) GetUint64WithDefault ¶
Get the value of the key as uint64 and if the key does not exist return the default value
func (*Section) GetUintWithDefault ¶
Get the value of the key as int and if the key does not exist return the default value
func (*Section) GetValueWithDefault ¶
Get value of key and if the key does not exist, return the defValue
func (*Section) HasKey ¶
check if the key is in the section
return true if the section contains the key