golang与pgsql交互的实现
作者:没枕头我咋睡觉 时间:2024-05-25 15:11:52
1、目的
通过读取配置文件获取pgsql用户名和密码及数据库和数据表,从而读取所需字段值。
2、代码架构
config/config.go:读取配置文件信息
config/config.json:配置文件
handle/handle.go:与pgsql数据库交互,获取所需数据
main.go:主函数
3、代码展示
config.go
package config
import (
"io/ioutil"
"os"
"fmt"
"encoding/json"
)
type Pgsql struct {
//Pgsql账号
Username string `json:"username"`
//Pgsql密码
Password string `json:"password"`
//Pgsql地址
Address string `json:"address"`
//Pgsql端口
Port int `json:"port"`
//数据库名称
Database string `json:"database"`
//表名称
Table string `json:"table"`
//账号字段名称
AccountField string `json:"accountField"`
//密码字段名称
PwdField string `json:"pwdField"`
}
//配置文件路径
var ConfigPath string = "config/config.json"
// pgsql配置信息缓存
var PgsqlData *Pgsql
func Init() {
//初始化配置对象
PgsqlData = new(Pgsql)
//读取配置文件
file, err := os.Open(ConfigPath)
if err != nil {
fmt.Println("config path:", err)
os.Exit(1)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("config file:", err)
os.Exit(1)
}
//使用json转换至config对象中
err = json.Unmarshal(bytes, PgsqlData)
if err != nil {
fmt.Println("json unmarshal:", err)
os.Exit(1)
}
}
config.json
{
"username": "***",
"password": "*********",
"address": "localhost",
"port": 5432,
"database": "***",
"table": "***",
"accountField": "***",
"pwdField": "***"
}
handle.go
package handle
import (
"database/sql"
"errors"
"fmt"
"pgsql/config"
_ "github.com/lib/pq"
)
func Executl() error {
config.Init()
if config.PgsqlData != nil {
//创建mysql连接
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", config.PgsqlData.Address, config.PgsqlData.Port, config.PgsqlData.Username, config.PgsqlData.Password, config.PgsqlData.Database))
if err != nil {
return err
}
defer db.Close()
str := fmt.Sprintf("select %s,%s from %s where NAME='%s'", config.PgsqlData.AccountField, config.PgsqlData.PwdField, config.PgsqlData.Table, config.PgsqlData.Username)
rows := db.QueryRow(str)
var name *sql.NullString
var pwd *sql.NullString
err = rows.Scan(&name, &pwd)
if err != nil {
return err
}
if name.String == "" || pwd.String == "" {
return errors.New("data is null")
}
fmt.Println("name:",name.String)
fmt.Println("passwd:",pwd.String)
return nil
}
return errors.New("open config file failed")
}
main.go
package main
import (
"pgsql/handle"
"os"
)
func main() {
err := handle.Executl()
if err != nil {
os.Exit(1)
}
}
来源:https://blog.csdn.net/qq_41982304/article/details/125905061
标签:golang,pgsql,交互
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php实现utf-8和GB2312编码相互转换函数代码
2024-04-29 13:56:12
能让Python提速超40倍的神器Cython详解
2023-11-12 07:11:11
![](https://img.aspxhome.com/file/2023/2/64662_0s.png)
Python PIL图片如何按比例裁剪
2021-08-13 02:04:55
![](https://img.aspxhome.com/file/2023/1/72041_0s.jpg)
浅谈python中的实例方法、类方法和静态方法
2022-02-10 07:11:27
XML DOM介绍和例子
2007-10-15 20:23:00
MySQL如何基于Explain关键字优化索引功能
2024-01-21 07:34:31
![](https://img.aspxhome.com/file/2023/3/129933_0s.png)
python 使用 with open() as 读写文件的操作方法
2021-05-08 08:32:20
![](https://img.aspxhome.com/file/2023/1/123471_0s.jpg)
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
2022-03-21 10:30:42
![](https://img.aspxhome.com/file/2023/0/123840_0s.jpg)
golang解析网页利器goquery的使用方法
2023-10-13 06:36:12
Python drop()删除行列的操作方法
2022-10-27 17:12:41
![](https://img.aspxhome.com/file/2023/3/69443_0s.png)
Python删除n行后的其他行方法
2022-07-01 15:06:29
混淆矩阵Confusion Matrix概念分析翻译
2022-07-05 15:08:56
![](https://img.aspxhome.com/file/2023/7/107167_0s.png)
Python hashlib加密模块常用方法解析
2022-03-11 05:20:05
Python程序中的观察者模式结构编写示例
2022-08-04 22:12:11
![](https://img.aspxhome.com/file/2023/3/66823_0s.png)
有趣的python小程序分享
2023-11-27 20:31:55
![](https://img.aspxhome.com/file/2023/7/98787_0s.png)
Python数据库编程之pymysql详解
2024-01-20 03:16:19
![](https://img.aspxhome.com/file/2023/7/90817_0s.png)
详解Python的Django框架中的模版相关知识
2023-04-22 02:46:25
解决Element中el-date-picker组件不回填的情况
2024-04-09 11:00:47
![](https://img.aspxhome.com/file/2023/7/139677_0s.jpg)
ASP控制每页打印行数实例
2008-04-13 06:48:00
js刷新页面方法大全
2023-08-06 21:05:34