I googled it but they are showing to change the ip of system. But I need to change for my particular web application as I have a config file in that I have tagged with ip port number as shown DB_info type ="postgres" ip="10.11.0.17" port="5432" but for every time I need to change the ip for other system.
So I need to make it as dynamic ip instead of static ip in golang.
It's hard to understand what you are really need but my telepathy skill says me that you just want to know how to load DB configuration from file. If I'm right there is solution.
Your config.xml
<config>
<DB_info type ="postgres" ip="10.11.0.17" port="5432" />
</config>
Code for this 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)
}
You can use config.DBInfo
elements as you wish - init DB, display to user, etc. More about XML parsing in Go here.