package ini import ( "fmt" ) type elementType int const ( section elementType = 1 << iota variable elementType = 1 << iota ) type element struct { eType elementType eKey []byte eVal []byte } func newElement() *element { return &element{} } func (e *element) setValue(v interface{}) bool { switch v.(type) { case byte: e.eVal = append(e.eVal, v.(byte)) return true case []byte: e.eVal = v.([]byte) return true } fmt.Printf("Uknown type: %T", v) return false } func (e *element) removeLastValueByteMatching(c byte) { if n := len(e.eVal); n != 0 && e.eVal[n-1] == c { e.eVal = e.eVal[0 : n-1] } } func (e *element) setKey(k interface{}) bool { switch k.(type) { case byte: e.eKey = append(e.eKey, k.(byte)) return true case []byte: e.eKey = k.([]byte) return true } return false } func (e *element) removeLastKeyByteMatching(c byte) { if n := len(e.eKey); n != 0 && e.eKey[n-1] == c { e.eKey = e.eKey[0 : n-1] } } func (e *element) setType(t elementType) { e.eType = t } func (e *element) String() string { switch e.eType { case section: return fmt.Sprintf( "section: %s", e.eKey, ) case variable: if len(e.eVal) == 0 { return fmt.Sprintf( "variable: %s -> true", e.eKey, ) } return fmt.Sprintf( "variable: %s -> %s", e.eKey, e.eVal, ) default: return "unknown element" } }