Giter VIP home page Giter VIP logo

ini's Introduction

ini file for Go Build Status

This module is a ini file parser for Golang. It tries to have an API as close as possible to the standard library.

Full documentation is available at

https://godoc.org/github.com/claudetech/ini

Usage

Here is a sample usage, using the wrapper to read files.

package main

import (
  "fmt"
  "github.com/claudetech/ini"
)

func main() {
  var conf ini.Config
  if err := ini.DecodeFile("/path/to/ini", &conf); err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(conf)
}

The type ini.Config is only an alias for

map [string]map[string]string

Note that you can pass any interface to receive the result as long as it is supported by the mapstructure package.

The more general version uses the ini.Decoder structure. A ini.Decoder can be created with ini.NewDecoder and takes anything that responds to the io.Reader interface.

package main

import (
  "fmt"
  "github.com/claudetech/ini"
  "os"
)

func exitError(err error) {
  fmt.Println(err)
  os.Exit(1)
}

func main() {
  var conf ini.Config
  file, err := os.Open("/etc/php/php.ini")
  if err != nil {
    exitError(err)
  }
  d := ini.NewDecoder(file)
  d.IdRegexp("^[a-z][a-z0-9_\\. -]+$")
  if err = d.Decode(&conf); err != nil {
    exitError(err)
  }
  fmt.Println(conf)
}

Configuration

There are several configuration options for the decoder.

type Options struct {
  IdRegexp     string // default: "^[a-z][a-z0-9_]+$"
  SepChars     []byte // default: []byte{'='}
  CommentChars []byte // default: []byte{';'}
  LowCaseIds   bool   // default: true
}

You can pass an ini.Options to ini.NewDecoderWithOptions, or you can set it directly on the ini.Decoder object through the setters of the same name.

For example, if you want ; and # to stand for comments, : and = to be separators, and sections keys to contain anything, and the keys not to be lower-cased, you could write the following.

options := ini.Options{".*", []byte{'=', ':'}, []byte{';', '#'}, false}
d := ini.NewDecoderWithOptions(file, options)

ini's People

Contributors

danhper avatar

Stargazers

Ivan Roslov avatar Claudson Oliveira avatar

Watchers

James Cloos avatar  avatar Kory Yahagi avatar Yuma avatar Yuya Soneoka avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.