【问题标题】:How to change static ip to dynamic ip in web application using golang?如何使用 golang 将 Web 应用程序中的静态 ip 更改为动态 ip?
【发布时间】:2016-05-09 16:27:21
【问题描述】:

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

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

【问题讨论】:

  • 你曾经问过这个问题。 stackoverflow.com/questions/35084208/…
  • 是的,但我没有得到答案
  • 真的不清楚你在问什么。如果您正在寻找动态端点管理系统,请查看 consul consul.io
  • c 我有一个动态配置文件,我在那里指定数据库类型、IP、端口,如下所示

标签: go


【解决方案1】:

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

你的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多