1. 程式人生 > >Go語言資料整理

Go語言資料整理

結構體格式化輸出

package main
import (
    "flag"
    "fmt"
    "bytes"
    "encoding/json"
)
type RedisConfig struct {
    IP  string
    PORT  string
    AUTH       int
    PASS string
}
type DbConfig struct {
    Host   string
    Port   int
    Uid    string
    Pwd    string
    DbName string
}
//Config 遊戲伺服器的配置
type Config struct { ServerId int Port int //埠號 Redis *RedisConfig DbConfigs map[string]*DbConfig //如果配置多個數據庫源,則用逗號分隔源的名字 callbacks []func() } func (conf *Config) String() string { b, err := json.Marshal(*conf) if err != nil { return fmt.Sprintf("%+v", *conf) } var
out bytes.Buffer err = json.Indent(&out, b, "", " ") if err != nil { return fmt.Sprintf("%+v", *conf) } return out.String() } func main(){ conf:=Config{ ServerId:1, Port:8080, Redis:&RedisConfig{ IP:"127.0.0.1", PORT:"3679"
, }, DbConfigs: map[string]*DbConfig{ "maindb": &DbConfig{ Host:"127.0.0.1", } , }, } fmt.Println("Config:",conf.String()) }