解析yaml配置文件,并生成json数据

package main

import (

"encoding/base64"

"encoding/json"

"fmt"

"io/ioutil"

"gopkg.in/yaml.v2"

)

//目录文件

var (

cfgFile = "./config.yml"

)

type proxies struct {

Name string `yaml:"name,omitempty",json:"name,omitempty"`

Type string `yaml:"type,omitempty",json:"type,omitempty"`

Server string `yaml:"server,omitempty",json:"server,omitempty"`

Port string `yaml:"port,omitempty",json:"port,omitempty"`

Cipher string `yaml:"cipher,omitempty",json:"cipher,omitempty"`

Password string `yaml:"password,omitempty",json:"password,omitempty"`

Udp string `yaml:"udp,omitempty",json:"udp,omitempty"`

}

type conf struct {

Proxies []proxies `yaml:"proxies",json:"proxies"`

}

func (c *conf) getConf() *conf {

yamlFile, err := ioutil.ReadFile(cfgFile)

if err != nil {

fmt.Println(err.Error())

}

err = yaml.Unmarshal(yamlFile, c)

if err != nil {

fmt.Println(err.Error())

}

return c

}

func main() {

var c conf

conf := c.getConf()

//proxie to json

jsonProxie, _ := json.Marshal(conf.Proxies)

fmt.Println(string(jsonProxie))

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值