当前位置:网站首页>Golang对JSON文件的写操作
Golang对JSON文件的写操作
2022-06-23 15:38:00 【学亮编程手记】
Go语言JSON文件的读写操作
package main
import (
"encoding/json"
"fmt"
"os"
)
type Website struct {
Name string `xml:"name,attr"`
Url string
Course []string
}
func main() {
info := []Website{
{
"Golang", "http://c.biancheng.net/golang/", []string{
"http://c.biancheng.net/cplus/", "http://c.biancheng.net/linux_tutorial/"}}, {
"Java", "http://c.biancheng.net/java/", []string{
"http://c.biancheng.net/socket/", "http://c.biancheng.net/python/"}}}
// 创建文件
filePtr, err := os.Create("info.json")
if err != nil {
fmt.Println("文件创建失败", err.Error())
return
}
defer filePtr.Close()
// 创建 json 编码器
encoder := json.NewEncoder(filePtr)
err = encoder.Encode(info)
if err != nil {
fmt.Println("编码错误", err.Error())
} else {
fmt.Println("编码成功")
}
}
info.json:
边栏推荐
- npm install 问题解决(nvm安装与使用)
- Apple iPhone, Samsung mobile phone and other electronic products began to enter Russia through parallel import channels
- MES在流程和离散制造企业的15个差别(上)
- VGG下载(.net文件和imagenet-vgg-verydeep-19)
- 港股多支个股表现活跃,引发投资者对港股市场回暖猜想与关注
- TCP protocol notes
- 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- Asynclistener interface of servlet 3.0
- How can I get the discount for opening a securities account? Is online account opening safe?
猜你喜欢

机器人方向与高考选专业的一些误区

SaaS cloud tool, a sharp tool for change under the industrial Internet

513. Find Bottom Left Tree Value

万字攻略,详解腾讯面试(T1-T9)核心技术点,面试题整理

Advanced development - generic entry basic class test
Drag the child file to the upper level

Uniapp sends picture messages to Tencent instant messaging Tim

stylegan1: a style-based henerator architecture for gemerative adversarial networks

【TcaplusDB知识库】TcaplusDB新增机型介绍

阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一文搞定
随机推荐
[tcapulusdb knowledge base] Introduction to tmonitor system upgrade
ADB key name, key code number and key description comparison table
XML
Asynclistener interface of servlet 3.0
批量注册组件
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
ADB 按键名、按键代码数字、按键说明对照表
现在我要买股票,怎么开户?手机开户安全么?
《ThreadLocal》
怎样快速的应对变动的生产管理需求?
js中 if 直接判断 数据类型 结果举例
R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图(bar plot)可视化股票月收益率数据、使用百分比显示Y轴坐标数据
Spin lock using CAS
Object
pytorch:模型的保存与导出
MATLAB中iscellstr函数的使用
139. Séparation des mots
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
A tour of gRPC:01 - 基础理论
Apache commons tool class