Skip to content

zooyer/properties

Repository files navigation

The Go Properties

Go Properties is migrate java Properties.

Download and Install

go get github.com/zooyer/properties

Features

  • 100% compatible with java Properties
  • xml properties support
  • persistence support

Example

  1. set/get/list

    package main
    
    import (
    	"fmt"
    	"github.com/zooyer/properties"
    )
    
    func main() {
    	prop := properties.NewProperties()
    
    	prop.SetProperty("title", "properties")
    	prop.SetProperty("language", "golang")
    	prop.SetProperty("version", "1.9.2")
    
    	fmt.Println(prop.GetProperty("title"))
    	fmt.Println(prop.GetProperty("language"))
    	fmt.Println(prop.GetProperty("version"))
    
    	prop.List(os.Stdout)
    }

    output:

    properties true
    golang true
    1.9.2 true
    -- listing properties --
    title = properties
    language = golang
    version = 1.9.2
  2. store

    package main
    
    import (
    	"fmt"
    	"github.com/zooyer/properties"
    	"os"
    )
    
    func main() {
    	prop := properties.NewProperties()
    
    	prop.SetProperty("title", "properties")
    	prop.SetProperty("language", "golang")
    	prop.SetProperty("version", "1.9.2")
    
    	file, err := os.Create("test.properties")
    	if err != nil {
    		panic(err)
    	}
    	defer file.Close()
    
    	if err = prop.Store(file, []byte("golang properties test comment")); err != nil {
    		panic(err)
    	}
    	file.Seek(0, io.SeekStart)
    
    	prop2 := properties.NewProperties()
    	if err = prop2.Load(file); err != nil {
    		panic(err)
    	}
    	prop2.List(os.Stdout)
    }

    output:

    -- listing properties --
    title = properties
    language = golang
    version = 1.9.2

    test.propertes:

    #golang properties test comment
    # Sun Jul 21 12:42:11 CST 2019
    language = golang
    version = 1.9.2
    title = properties
  3. xml

    package main
    
    import (
    	"github.com/zooyer/properties"
    	"os"
    )
    
    func main() {
    	prop := properties.NewProperties()
    
    	prop.SetProperty("title", "properties")
    	prop.SetProperty("language", "golang")
    	prop.SetProperty("version", "1.9.2")
    
    	prop.StoreToXMLByEncoding(os.Stdout, []byte("test xml comment"), "utf-8")
    }

    output:

    <!DOCTYPE properties SYSTEM "http://github.com/zooyer/properties">
    <properties>
        <comment>test xml comment</comment>
        <entry key="version">1.9.2</entry>
        <entry key="title">properties</entry>
        <entry key="language">golang</entry>
    </properties>

Releases

No releases published

Packages

No packages published

Languages