Code Monkey home page Code Monkey logo

config's Introduction

Usage

create new yaml config as below and save as config.yaml file:

mode: develop
addr: localhost
port: 8080
enable: true
pubdir: static
lang: zh-tw
db:
  driver: MySQL
  Protocol: tcp
  host: localhost
  username: root
  password: 123456
  database: test
  enable: true
  port: 3306
  isTrue: "F"
  isFalse: 1
  average: 2.0
  coverage: 2.1
  gpa: "3.7"
  floatzero: 0.0

example.go

c := &config.Engine{}
c.Load("config.yaml")

// Result: Mysql (string)
fmt.Println(c.Get("db.driver"))
// Result: 3306 (int)
fmt.Println(c.Get("db.port"))
// Result: true (bool)
fmt.Println(c.Get("db.enable"))
// Result: 2.0 (float64)
fmt.Println(c.Get("db.average"))

// Result: Mysql (string)
fmt.Println(c.GetString("db.driver"))
// Result: 3306 (string)
fmt.Println(c.GetString("db.port"))
// Result: true (string)
fmt.Println(c.GetString("db.enable"))
// Result: 2 (string)
fmt.Println(c.GetString("db.average"))
// Result: 2.1 (string)
fmt.Println(c.GetString("db.coverage"))

// Result: 0 (int)
fmt.Println(c.GetInt("db.driver"))
// Result: 3306 (int)
fmt.Println(c.GetInt("db.port"))
// Result: 1 (int)
fmt.Println(c.GetInt("db.enable"))
// Result: 2 (int)
fmt.Println(c.GetInt("db.average"))
// Result: 2 (int)
fmt.Println(c.GetInt("db.coverage"))

// Result: false (bool)
fmt.Println(c.GetBool("db.driver"))
// Result: true (bool)
fmt.Println(c.GetBool("db.port"))
// Result: true (bool)
fmt.Println(c.GetBool("db.enable"))
// Result: true (bool)
fmt.Println(c.GetBool("db.average"))
// Result: true (bool)
fmt.Println(c.GetBool("db.coverage"))

Get Struct

type SessionConfig struct {
	CookieName      string
	SessionIDLength int
	CookieLifeTime  int // cookie expire time
	ExpireTime      int // time for destroy from GC
	GCTime          int // GC frequency
	Domain          string
}

We can also defined in config.yaml, and use GetStruct function to get SessionConfig struct, example:

mode: develop
addr: localhost
port: 8080
enable: true
pubdir: static
lang: zh-cn
db:
  driver: MySQL
  Protocol: tcp
  host: localhost
  username: root
  password: 123456
  database: test
  enable: true
  port: 3306
  isTrue: "F"
  isFalse: 1
sessions:
  CookieName: "test-cookie"
  SessionIDLength: 10
  CookieLifeTime: 3600
  ExpireTime: 3600
  GCTime: 10
  Domain: example.com

example.go

c := &config.Engine{}
c.Load("config.yaml")

// Result: &{
//  CookieName:test-cookie
//  SessionIDLength:10
//  CookieLifeTime:3600
//  ExpireTime:3600
//  GCTime:10
//  Domain:example.com
//}
fmt.Printf("%+v", c.GetStruct("sessions", &sessions.SessionConfig{}))

config's People

Contributors

ego008 avatar

Watchers

 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.