douhoujun9304 2018-01-04 13:18
浏览 112

Golang可执行文件刷新

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

  • 写回答

2条回答 默认 最新

  • duanliao3826 2018-01-04 18:15
    关注

    Its quite difficult to understand exactly what you want. But I suspect that is essence you simply want to extract the output from a shell command and write it to JSON.

    For this there is no need to modify the Handler.go file or do go build. You can simply write the output directly into the GetHand structure.

    A basic example is as follows :

        package main
    
        import (
            "fmt"
            "os/exec"
        )
    
        var cmds = map[string][]string{
            "go":   []string{"/usr/local/go/bin/go", "version"},
            "java": []string{"java", "-version"},
        }
    
        type GetHand struct {
            Name    string
            Version string
        }
    
        func handleKey(key string) (*GetHand, error) {
    
            cmd := cmds[key]
            if cmd == nil {
                return nil, fmt.Errorf("No such key : %v", key)
            }
    
            b, err := exec.Command("/usr/local/go/bin/go", "version").Output()
            if err != nil {
                return nil, err
            }
            return &GetHand{
                Name:    key,
                Version: string(b),
            }, nil
        }
    
        func main() {
            h, err := handleKey("go")
            if err != nil {
                fmt.Println(err)
                return
            }
            fmt.Println(h)
    
            h, err = handleKey("java")
            if err != nil {
                fmt.Println(err)
                return
            }
            fmt.Println(h)
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭