I have a function which writes/updates a json. But I need to stop the executable, run go build again and re-run the executable to get the json updated in url.
For example, I have a Handler.go file which takes argument from URL as key and runs an if condition and updates the json. So If json value before building the executable is {"Name":"Sneha"} and i pass parameter "Nair" in the url, the json gets updated in the server as {"Name":"Nair"}, but doesnt get updated in the URL. So I have to stop the executable, run go build again and run the executable again to reflect the new json value {"Name":"Nair"} in the URL.
1. Can somebody please suggest an alternative idea ?
2. Can we run go build or go update inside a function?
Help much appreciated.
PS: I have got URL for goagain.go. But am not sure if that matches my requirement.
Handler.go
func handler(w http.ResponseWriter, r *http.Request) {
keys, ok := r.URL.Query()["key"]
if !ok || len(keys) < 1 {
log.Println("Url Param 'key' is missing")
return
}
key := keys[0]
log.Println("Url Param 'key' is: " + string(key))
if key == "java" {
commands := []string{
"Version=`java -version`",
"sed -i -e 's/^\\( *Name: *\\) .*$/ Name:\"Java\",/' Handler.go",
"sed -i -e 's/^\\( *Version: *\\) .*$/ Version:\" '$Version'\",/' Handler.go",
}
exe_cmd(commands)
}
if key == "go" {
commands := []string{
"Version=`go version`",
"sed -i -e 's/^\\( *Name: *\\) .*$/ Name:\"Go\",/' Handler.go",
"sed -i -e 's/^\\( *Version: *\\) .*$/ Version:\" '$Version'\",/' Handler.go",
}
exe_cmd(commands)
}
GetHands := GetHand{
Name:"java",
Version:" 1.7.0_71",
}
if err := json.NewEncoder(w).Encode(GetHands); err != nil {
panic(err)
}
So on running this package, the url shows json value : {"name":"java","version":" 1.7.0_71"} If I call url : http://localhost:8080/?key=go this Handler.go gets updated to,
GetHands := GetHand{
Name:"go",
Version:" 1.9",
}
If I stop the executable, run go build again and run executable again the url gets returned as :{"name":"go","version":" 1.9"}
So basically I need dynamic url which on hitting the http:/localhost/?key=go would return go's corresponding value annd htpp://localhost/?key=java would return java's corresponding value. This should be attained without restarting the executable or re-running the go build