Reading an .ini file

2,432 views
Skip to first unread message

buc...@gmail.com

unread,
Jan 14, 2017, 11:46:47 AM1/14/17
to golang-nuts
Rankest beginner here.  Trying to learn Go by converting an old Visual Basic application to Go (yeah, right) on Windows 10 (got all the database stuff working with both postgre and sqlite!)  The application needs to read an .ini file to get path/filename.  It bombs on the first line.

This is from my testing platform.  I like doing things in baby steps, please.

package main

import (
"fmt"

)

func main() {
pathfilename := readINIfile()
fmt.Println(pathfilename)
}

func readINIfile() string {
_, err := ini.LoadFile("C:\\temp\\testfile.ini")
if err != nil {
fmt.Println(err)
}
       return ""
}

I can't get beyond the first err test.  The error returned is: "invalid INI syntax on line 1: ;blah".  It is choking on the ';blah'.  Also chokes on a blank line and on simply the ';' character.  The author's documentation says:

INI files are parsed by go-ini line-by-line. Each line may be one of the following:

  • A section definition: [section-name]
  • A property: key = value
  • A comment: #blahblah or ;blahblah
  • Blank. The line will be ignored.
I've stared at this for hours.  Any idea what I'm missing?

Nathan Kerr

unread,
Jan 14, 2017, 12:43:55 PM1/14/17
to golang-nuts
Using a newer package might help.

https://github.com/go-ini/ini looks to be under active development and is written by the author of https://github.com/Unknwon/goconfig, which also reads ini files but is now in bug fix only mode.

Both claim to support comments.

Message has been deleted

mhh...@gmail.com

unread,
Jan 14, 2017, 12:55:24 PM1/14/17
to golang-nuts
I can reproduce your problem by inserting the BOM in front of the file.

printf '\xEF\xBB\xBF;blah\nff=ff' > test.ini

A quick and dirty fix might look like this,
it wraps the read operation with a bomskipreader,
which jobs is to detect and get ride of the bom if detected,
thus its like it was never there was when the vendor read lines
at https://github.com/vaughan0/go-ini/blob/master/ini.go#L76



package main

import (
    "fmt"
    "io"
    "os"


    ini "github.com/vaughan0/go-ini"
)

func main() {
    file, err := os.Open("test.ini")
    if err != nil {
        panic(err)
    }
    ini, err := ini.Load(&bomSkipReader{R: file})
    if err != nil {
        panic(err)
    }
    fmt.Println(ini)
}

// the quick and dirty way.

type bomSkipReader struct {
    R    io.Reader
    sent bool
}

func (l bomSkipReader) Read(p []byte) (int, error) {
    n, err := l.R.Read(p)
    if l.sent == false {
        if len(p) > 3 {
            if p[0] == 0xef && p[1] == 0xbb && p[2] == 0xbf {
                p = append(p[:0], p[3:]...)
                n -= 3
            }
            l.sent = true
        }
    }
    fmt.Printf("%#v\n", p[:5])
    return n, err
}

PS: something is flaky with the editor of this NG :x

buc...@gmail.com

unread,
Jan 14, 2017, 9:00:59 PM1/14/17
to golang-nuts

Thank you all for the suggestions.  Some of them are WAAAAAY above my pay grade presently.  I'll try one of the other suggested packages.
 

On Saturday, January 14, 2017 at 9:46:47 AM UTC-7, buc...@gmail.com wrote:

hui zhang

unread,
Jan 16, 2017, 9:52:49 PM1/16/17
to golang-nuts
this package is quit buggy 
check its issues,   I list some , and still not fix yet

在 2017年1月15日星期日 UTC+8上午1:43:55,Nathan Kerr写道:

Nathan Kerr

unread,
Jan 17, 2017, 2:13:21 AM1/17/17
to golang-nuts
Do you have a better suggestion?

Konstantin Khomoutov

unread,
Jan 17, 2017, 3:37:57 AM1/17/17
to buc...@gmail.com, golang-nuts
On Sat, 14 Jan 2017 08:03:45 -0800 (PST)
buc...@gmail.com wrote:

> Rankest beginner here. Trying to learn Go by converting an old
> Visual Basic application to Go (yeah, right) on Windows 10 (got all
> the database stuff working with both postgre and sqlite!) The
> application needs to read an .ini file to get path/filename. It
> bombs on the first line.

I would recommend [1] and [2]. Successfully using both in production.

I like [1] because it's very simple and well engeneered at the same
time. The upstream author was responsive and helpful as well when I
chimed in his bugtracker.

gcfg has an advantage of supporting some level of hierarchy by allowing
you to have named groups of settings (which have the same "shape").

Obviously, more hard-core solution exist, such as a TOML parsing
package (which, as I heard is a very good shape) and YAML parsers.
Stock Go is also able to parse XML and JSON.

Still, for my personal needs those two packages provided the minimal
amount of functionality to feel not overwhelmed, so I like them.

1. https://github.com/Thomasdezeeuw/ini
2. https://gopkg.in/gcfg.v1

Konstantin Khomoutov

unread,
Jan 17, 2017, 3:42:42 AM1/17/17
to Nathan Kerr, golang-nuts
On Sat, 14 Jan 2017 09:43:54 -0800 (PST)
Nathan Kerr <nat...@pocketgophers.com> wrote:

> Using a newer package might help.
>
> https://github.com/go-ini/ini looks to be under active development
> and is written by the author of https://github.com/Unknwon/goconfig,
> which also reads ini files but is now in bug fix only mode.

This library is a classical case of overengeenering beyold all
repair/recognition (OEBAR, if you want). Like, "OK, let's cram
everything I learned about programming in Go so far into this library
and try to make it fly" mentality. Check the dreaded BlockMode
variable for just one glaring example. I think such libraries
represent the clear antithesis to the very ideas which make Go what it
is, and should be avoided.

pierre...@gmail.com

unread,
Jan 30, 2017, 1:33:50 PM1/30/17
to golang-nuts, nat...@pocketgophers.com
Hello,

When this thread came up I was working on an ini formatting tool, similar to (but way simpler than) gofmt, which required parsing and manipulating the contents of an ini file. As you have found out, there are a bunch of packages already doing that, but not in the spirit of simplicity that Go conveys, as well as what I wanted to do with comments for ini sections and keys.

So I went to write my own, and ended up with https://github.com/pierrec/go-ini, which solves all the pain points I had.

Feel free to provide any feedback!
Reply all
Reply to author
Forward
0 new messages