golang oracle配置文件,Golang读取并修改非主流配置文件-Go语言中文社区

该博客讲述了如何使用Go语言的ini包解析和修改MySQL和PostgreSQL配置文件,重点是通过`strings.Replace()`函数替换特定键值radius_db的内容。作者提供了一段示例代码来演示操作过程。
摘要由CSDN通过智能技术生成

今天工作中碰到的问题,要求修改此配置文件,没看出来是什么格式,用了下面的思路:

mysql {

# If any of the files below are set, TLS encryption is enabled

tls {

ca_file = "/etc/ssl/certs/my_ca.crt"

ca_path = "/etc/ssl/certs/"

certificate_file = "/etc/ssl/certs/private/client.crt"

private_key_file = "/etc/ssl/certs/private/client.key"

cipher = "DHE-RSA-AES256-SHA:AES128-SHA"

tls_required = yes

tls_check_cert = no

tls_check_cert_cn = no

}

# If yes, (or auto and libmysqlclient reports warnings are

# available), will retrieve and log additional warnings from

# the server if an error has occured. Defaults to 'auto'

warnings = auto

}

postgresql {

# unlike MySQL, which has a tls{} connection configuration, postgresql

# uses its connection parameters - see the radius_db option below in

# this file

# Send application_name to the postgres server

# Only supported in PG 9.0 and greater. Defaults to no.

send_application_name = yes

}

# Connection info:

#

server = "127.0.0.1"

port = 5432

login = "testuser13"

password = "Tpass123"

# Database table configuration for everything except Oracle

radius_db = "test13"

# If you are using Oracle then use this instead

# radius_db = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=your_sid)))"

用Go代码修改如上所示的配置文件,如radius_db字段的值,步骤如下:

github.com/go-ini/ini获取键radius_db的值;

fmt.Sprintf()将radius_db = "test13"整体包装成字符串;

fmt.Sprintf()包装新字符串;

strings.Replace()整体替换。

主要实现代码:

// 根据路径获取文件

configFile := "./sql.conf"

configContent, err := ioutil.ReadFile(configFile)

// 加载配置文件,跳过无法解析的行;设置key/value分隔符为"="

cfg, err := ini.LoadSources(ini.LoadOptions

{SkipUnrecognizableLines:true, KeyValueDelimiters:"="}, configFile)

// 获取"radius_db"的值

sqlDBName := cfg.Section("").Key("radius_db").String()

// newName指要修改的新值

sqlDBNameOld := fmt.Sprintf(`radius_db = "%s"`, sqlDBName)

sqlDBNameNew := fmt.Sprintf(`radius_db = "%s"`, newName)

// 替换并写入

newConfig := strings.Replace(string(configContent), sqlDBNameOld, sqlDBNameNew, 1)

// 写入文件

err = ioutil.WriteFile(configFile, []byte(newConfig), 0644)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值