Giter VIP home page Giter VIP logo

Comments (4)

niemeyer avatar niemeyer commented on August 17, 2024

You can use a field tag to tell exactly how the name should look like:

ReportSeconds int64 `yaml:"reportSeconds"`

from yaml.

niemeyer avatar niemeyer commented on August 17, 2024

By the way, there's full documentation for these options at:

http://gopkg.in/yaml.v2#Marshal

from yaml.

vrecan avatar vrecan commented on August 17, 2024

Oh sorry, thanks for the help!

from yaml.

shyandsy avatar shyandsy commented on August 17, 2024

You can use a field tag to tell exactly how the name should look like:

ReportSeconds int64 `yaml:"reportSeconds"`

it doesnt work

the yaml file:

max: 1
maxIdleConnections: 10
max_open_connections: 10

the demo code here:

package main

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
)

func main() {
	
	
	// 加载配置
	var d Database
	d.LoadConfig("config.yaml")
	
	fmt.Println(d.Max)
	fmt.Println(d.MaxOpenConnections)
	fmt.Println(d.MaxOpenConnections)
}

// 映射 总配置信息
type Database struct {
	Max	                int		    `yaml: "max"`
	MaxIdleConnections	int64       `yaml: "maxIdleConnections"`
	MaxOpenConnections	int64       `yaml: 'max_open_connections'`
}

func (configuration *Database)LoadConfig(filename string){
	//应该是 绝对地址
	yamlFile, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println(err.Error())
	}
	err = yaml.Unmarshal(yamlFile, configuration)
	if err != nil {
		fmt.Println(err.Error())
	}
}

and the output here

1
0
0

explain: the first line which key is max was parse correctly, and it doesnt work for yaml: "maxIdleConnections" and yaml: 'max_open_connections'

from yaml.

Related Issues (20)

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.