gpt4 book ai didi

go - 如何使用golang在Web应用程序中将静态IP更改为动态IP?

转载 作者:数据小太阳 更新时间:2023-10-29 03:47:20 29 4
gpt4 key购买 nike

我在谷歌上搜索过,但他们显示要更改系统的 ip。但是我需要针对我的特定 Web 应用程序进行更改,因为我有一个配置文件,我已经用 ip 端口号标记了 DB_info type ="postgres"ip="10.11.0.17" port="5432"但每次我都需要更改其他系统的 ip。

所以我需要在 golang 中将其设为动态 ip 而不是静态 ip。

最佳答案

很难理解您真正需要什么,但我的心灵感应技能告诉我,您只想知道如何从文件加载数据库配置。如果我是对的,就有解决方案。

你的config.xml

<config>
<DB_info type ="postgres" ip="10.11.0.17" port="5432" />
</config>

config.xml 的代码

package main

import (
"encoding/xml"
"log"
"os"
)

type Configuration struct {
DBInfo struct {
Type string `xml:"type,attr"`
IP string `xml:"ip,attr"`
Port int `xml:"port,attr"`
} `xml:"DB_info"`
}

func main() {
file, err := os.Open("config.xml")
if err != nil {
log.Panic(err)
}

config := Configuration{}
err = xml.NewDecoder(file).Decode(&config)
if err != nil {
log.Panic(err)
}

log.Println(config.DBInfo.IP)
}

您可以根据需要使用 config.DBInfo 元素 - 初始化数据库、向用户显示等。更多关于 Go 中 XML 解析的信息 here .

关于go - 如何使用golang在Web应用程序中将静态IP更改为动态IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126830/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com