package main import ( "encoding/json" "io/ioutil" "time" "strconv" "fmt" ) //要先定义json格式的结构体 type publishJson struct { BgId string `json:"bgId"` CandBgColor string `json:"candBgColor"` CurKbType int `json:"curKbType"` EffectId string `json:"effectId"` FontColor string `json:"fontColor"` FontColorChanged bool `json:"fontColorChanged"` FontId string `json:"fontId"` KeyId string `json:"keyId"` KeyTransparent float64 `json:"keyTransparent"` KeyboardBgName string `json:"keyboardBgName"` LocalSkinId string `json:"localSkinId"` Platform int `json:"platform"` PreviewName26 string `json:"previewName26"` PreviewName9 string `json:"previewName9"` SkinName string `json:"skinName"` SoundId string `json:"soundId"` UgcSkinVersion int `json:"ugcSkinVersion"` UseCustomBg bool `json:"useCustomBg"` } func main() { updateFile := "theme_publish.json" now := strconv.FormatInt(time.Now().UnixNano(), 10) write(now, updateFile) } func write(now string, path string) error{ var loacl string = now //初始化结构体内的json字段内容 theme_publish1 := publishJson{ BgId: "351", CandBgColor: "0xdffffcfc", CurKbType: 2, EffectId: "-1", FontColor: "0xff000000", FontColorChanged: false, FontId: "-1", KeyId: "10", KeyTransparent: 59.025097, KeyboardBgName: "keyboard_bg.png", LocalSkinId: "loacl", Platform: 1, PreviewName26: "", PreviewName9: "square_preview.png", SkinName: "bbb", SoundId: "-1", UgcSkinVersion: 1, UseCustomBg: false, } theme_publish1.LocalSkinId = loacl //将结构体转为json格式 data, err := json.Marshal(theme_publish1) checkError(err) //将json格式的数据写入文件 err = ioutil.WriteFile(path, data, 0777) checkError(err) return nil } func checkError(err error) { if err != nil { fmt.Println(err) panic(err) } }