https://github.com/ryosama/go-ini
Simple Go librairy to deals with ini configuration files
https://github.com/ryosama/go-ini
go golang ini inifile lib library
Last synced: about 2 months ago
JSON representation
Simple Go librairy to deals with ini configuration files
- Host: GitHub
- URL: https://github.com/ryosama/go-ini
- Owner: ryosama
- Created: 2018-08-21T09:00:53.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-24T07:36:02.000Z (over 6 years ago)
- Last Synced: 2025-02-12T08:57:00.122Z (4 months ago)
- Topics: go, golang, ini, inifile, lib, library
- Language: Go
- Size: 14.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Go-ini
======Go library to deals with ini configuration files
Install
=======```bash
$ go get -u github.com/ryosama/go-ini
```Quick Start
===========```Go
// Load the library
import "github.com/ryosama/go-ini"// Create object
myIni := new(ini.Ini)// Load config file
if err := myIni.LoadFromFile("config.ini") ; err != nil {
panic("Unable to load configuration : " + err.Error())
}// Get the value
myHost, _ := myIni.Get("Server","host")// Set another value and save
myIni.Set("Server","host","127.0.0.1")
myIni.Save()
```Documentation
=======The documentation can be found here : https://godoc.org/github.com/ryosama/go-ini
Or export with this command
```bash
$ godoc github.com/ryosama/go-ini
```Creating an object
====
myIni := new(ini.Ini)Object properties
=========
// Last filename pass to Load or Save
Filename string// Add this string before section (default is "")
SectionPrefix string// Add this string before every items (default is " ")
ItemPrefix string// Add this string after every items (default is " ")
ItemSuffix string// Add this string before every values (default is " ")
ValuePrefix string// Add this string before every new sections (except the first one) (default is "\r\n")
SectionSeparator string// Add this string before every new item (except the first one) (default is "\r\n")
ItemSeparator string// If set to false, remove all the comments while saving (default is true)
WithComments bool// Caractere(s) from prefixing a comment (default is "; ")
CommentPrefix string
// contains filtered or unexported fields
Methods
======
- AddItemAdd an item. Returns true if success, false if item already exists
func (this *Ini) AddItem(section string, item string, value string) bool
--------------------
- AddItemCommentAdd a comment to an item, return true if succeed, false otherwise
func (this *Ini) AddItemComment(section string, item string, comment string) bool
--------------------
- AddSectionAdd a section. Returns true if success, false if section already exists
func (this *Ini) AddSection(section string) bool
--------------------
- AddSectionCommentAdd a comment to a setion, return true if succeed, false otherwise
func (this *Ini) AddSectionComment(section string, comment string) bool
--------------------
- DeleteItemDelete an item, return true if succes, false if the item does not exists
func (this *Ini) DeleteItem(section string, item string) bool
--------------------
- DeleteItemCommentDelete the comment number id, return true if succeed, false otherwise
func (this *Ini) DeleteItemComment(section string, item string, id int) bool
--------------------
- DeleteItemCommentsDelete all the comments of an item, return true if succeed, false otherwise
func (this *Ini) DeleteItemComments(section string, item string) bool
--------------------
- DeleteSectionDelete a section, return true if succes, false if the section does not exists
func (this *Ini) DeleteSection(section string) bool
--------------------
- DeleteSectionCommentDelete the comment number id, return true if succeed, false otherwise
func (this *Ini) DeleteSectionComment(section string, id int) bool
--------------------
- DeleteSectionCommentsDelete all the comments of a section, return true if succeed, false
otherwisefunc (this *Ini) DeleteSectionComments(section string) bool
--------------------
- ExistsAlias for ItemExists
func (this *Ini) Exists(section string, item string) bool
--------------------
- GetAlias for GetItem
func (this *Ini) Get(section string, item string) (string, bool)
--------------------
- GetItemReturns the items value of the ini file for a given section and item
(and true as second return value)If the item does not exists, return false as second return value
func (this *Ini) GetItem(section string, item string) (string, bool)
Example :
value, success := myini.GetItem("section1","item1")
--------------------
- GetItemCommentsReturn the comments, one per line, just before the item, return empty slice of string if item does not exists.
func (this *Ini) GetItemComments(section string, item string) []string
Example :
for _, com := range myIni.GetItemComments("mySection","myItem") {
print("Comment for myItem", com, "\n")
}
--------------------
- GetItemsReturns all the items of the ini file for a given section
func (this *Ini) GetItems(section string) []string
--------------------
- GetSectionCommentsReturn the comments, one per line, just before the section, return empty slice of string if section does not exists.
func (this *Ini) GetSectionComments(section string) []string
Example :for _, com := range sectionExists := myIni.GetSectionComments("mySection") {
print("Comment for mySection", com, "\n")
}
--------------------
- GetSectionsReturns all the sections of the ini file
func (this *Ini) GetSections() []string
--------------------
- ItemExistsReturns true or false if the item exists for the given section
func (this *Ini) ItemExists(section string, item string) bool
--------------------
- LoadFromFileRead ini format from a file
func (this *Ini) LoadFromFile(filename string) error
Example :err := myIni.LoadFromFile("config.ini")
--------------------
- LoadFromStringLoad data from a string pointer
func (this *Ini) LoadFromString(content *string)
Example :
content := `
[section1]
item1=value1`
myini.LoadFromString( &content )
--------------------Print the ini format into a formatted string
TIPS : You can set _SectionPrefix,ItemPrefix, ItemSuffix, ValuePrefix, SectionSeparator, ItemSeparator, WithComments, CommentPrefix_ to tweak format aspect
func (this *Ini) Print()
--------------------
- RenameItemRename an item. Returns true if success, false if section or item does not exists
func (this *Ini) RenameItem(section, oldName string, newName string) bool
--------------------
- RenameSectionRename a section. Returns true if success, false if section does not exists
func (this *Ini) RenameSection(oldName string, newName string) bool
--------------------
- SaveSave the ini format to a file
func (this *Ini) Save(params ...string) error
Example :err := myIni.Save() // use myIni.Filename to save
err := myIni.Save("new_config.ini") // use new_config.ini and set
myIni.Filename
--------------------
- SectionExistsReturns true or false if the section exists
func (this *Ini) SectionExists(section string) bool
--------------------
- SetAlias for SetItem
func (this *Ini) Set(section string, item string, value string) bool
--------------------
- SetItemSet the value of an item. Returns true if success, false section or item does not exists
func (this *Ini) SetItem(section string, item string, value string) bool
--------------------
- SetOrCreateSet a value for an item, create section and item if needed
func (this *Ini) SetOrCreate(section string, item string, value string)
--------------------
- SprintReturn the ini format into a formatted string
TIPS : You can set _SectionPrefix,ItemPrefix, ItemSuffix, ValuePrefix, SectionSeparator, ItemSeparator, WithComments, CommentPrefix_ to tweak format aspect
func (this *Ini) Sprint() string