当前位置: 首页 > news >正文

【REST2SQL】03 GO读取JSON文件

REST2SQL需要一些配置信息,用JSON文件保存,比如config.json

在这里插入图片描述

1 创建config.json配置文件

{"hostPort":"localhost:5217","connString":"oracle://blma:5217@127.0.0.1:1521/CQYH","_oracle":"oracle://blma:5217@127.0.0.1:1521/CQYH","_mysql":"mysql://","_mssql":"mssql://","_postgresql":"postgresql://","REST":"GET,POST,PUT,DELETE", "_REST":"GET,POST,PUT,DELETE", "SQL":"SELECT","_SQL":"SELECT,INSERT,UPDATE,DELETE"}

其中_下划线开头的作为注释项。

2 创建一个Gojson的控制台程序

2.1 引入json和ioutil包

import ("encoding/json""fmt""io/ioutil"
)

2.2 创建配置项结构体

// 配置结构体
type Config struct {DBType     string //数据库类型 :oracle、mysql等ConnString string `json:"connString"`HostPort   string `json:"hostPort"`REST       string `json:"REST"`SQL        string `json:"SQL"`
}

反引号包裹的为json文件的键。
5个键分别为数据库类型,数据库连接字符串,http主机和端口,REST可用方法,SQL可用语句。

2.3 主控main()函数

func main() {fmt.Println("Go Read JSON")bytes, err := ioutil.ReadFile("config.json")if err != nil {fmt.Println("读取json文件失败", err)return}conf := &Config{}err = json.Unmarshal(bytes, conf)if err != nil {fmt.Println("json解析失败", err)return}fmt.Println(conf)fmt.Println("DBType:", conf.DBType)fmt.Println("connString:", conf.ConnString)fmt.Println("hostPort:", conf.HostPort)fmt.Println("REST:", conf.REST)fmt.Println("SQL:", conf.SQL)
}

3 go读取json全部代码

// gojson project main.go
// go 读取 Json 文件测试
package mainimport ("encoding/json""fmt""io/ioutil"
)// 配置结构体
type Config struct {DBType     string //数据库类型 :oracle、mysql等ConnString string `json:"connString"`HostPort   string `json:"hostPort"`REST       string `json:"REST"`SQL        string `json:"SQL"`
}func main() {fmt.Println("Go Read JSON")bytes, err := ioutil.ReadFile("config.json")if err != nil {fmt.Println("读取json文件失败", err)return}conf := &Config{}err = json.Unmarshal(bytes, conf)if err != nil {fmt.Println("json解析失败", err)return}fmt.Println(conf)fmt.Println("DBType:", conf.DBType)fmt.Println("connString:", conf.ConnString)fmt.Println("hostPort:", conf.HostPort)fmt.Println("REST:", conf.REST)fmt.Println("SQL:", conf.SQL)
}

4 执行结果

Go Read JSON
&{ oracle://blma:5217@127.0.0.1:1521/CQYH localhost:5217 GET,POST,PUT,DELETE SELECT}
DBType:
connString: oracle://blma:5217@127.0.0.1:1521/CQYH
hostPort: localhost:5217
REST: GET,POST,PUT,DELETE
SQL: SELECT

http://www.lryc.cn/news/275355.html

相关文章:

  • 数据库-MySQL 启动方式
  • YAML使用
  • 读书之深入理解ffmpeg_简单笔记2(初步)
  • ELK+kafka+filebeat企业内部日志分析系统搭建
  • 勒索检测能力升级,亚信安全发布《勒索家族和勒索事件监控报告》
  • 编译原理复习的有用链接
  • 不带控制器打包exe,转pdf文件时失败的原因
  • Python 注释的方法
  • webman插件创建
  • 大模型迎来“AppStore时刻”,OpenAI给2024的新想象
  • ubuntu解决在pycharm上使用jupyter无法导入虚拟环境中的包的问题
  • 探索2024年软件测试的几大主导趋势
  • Linux C语言 48-信号总结
  • 【vue技巧】之如何让mixin的data 比本身vue的data优先级要高
  • 全解析阿里云Alibaba Cloud Linux镜像操作系统
  • 什么是数据结构?
  • GOOS=darwin 代表macOS环境
  • hfish蜜罐docker部署
  • 我的创作纪念日——redis的历史纪录
  • 【Bootstrap5学习 day10】
  • 2024年学习计划
  • 学习笔记:C++之 switch语句
  • C++ 具名要求-全库范围的概念
  • 分布式(4)
  • XCTF-Misc1 USB键盘流量分析
  • 毛概笔记。
  • postman使用方法指南,最全面的教程
  • 工业物联网上篇——什么是IIOT?
  • 【JavaEE】Java多线程状态-- 多线程篇(10)
  • 坐标转换 | EXCEL中批量将经纬度坐标(EPSG:4326)转换为墨卡托坐标(EPSG:3857)