struct解析为json

一、通过decode来解析

package Controller

import (
	"bufio"
	"encoding/json"
	"os"
)

type App struct {
	App_port string `json:"app_port"`
}
type APPName struct {
	App_host string `json:"app_host"`
	App_addr string `json:"app_addr"`
	App App `json:"app"`
}

type Name struct {
	App_name APPName `json:"app_name"`
}

var name *Name

func Unmshare(path string) (*Name,error){

	file, err :=os.Open(path)
	if err !=nil{
		panic(err)
	}
	reader :=bufio.NewReader(file)
	decoder :=json.NewDecoder(reader)
	if err =decoder.Decode(&name);err !=nil{
		return nil,err
	}

	return name,nil
}

  

原文地址:https://www.cnblogs.com/wuchangblog/p/14909055.html