duanfu3390 2019-03-26 22:34
浏览 36
已采纳

GoLang的REST路由器方法中的调用对象方法

I have just started development of a REST api with GoLang. I have a class with some methods in the backend. The rest api should call one of the methods of the class and return the json response. I am having problem calling the object method or passing the object by reference. My cods looks like this.

package main

import (
    "time"
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/mux"
    "./objects")

/*Global Variables*/
var host objects.HostOS  // I have declared this to see if I can have a global variable and later on assign the actual object to that and call that object in the GetStats router method below. 

func main() {
    fmt.Println("Hello World")

    hostConfig := objects.HostConfig{CPUConfig: cpuConfig, MemoryKB: 4096, OSMemoryKB: 1024, OSCompute: 100}

    host := new(objects.HostOS)
    host.Init(hostConfig)

    host.Boot()
    time.Sleep(3 * time.Second)
    process := new(objects.Process)
    process.Init(objects.ProcessConfig{MinThreadCount: 2, MaxThreadCount: 8, ParentOSInstance: host})
    process.Start()

    router := mux.NewRouter().StrictSlash(true)

    router.HandleFunc("/", Index)
    router.HandleFunc("/get_os_stats", GetOSStats)

    log.Fatal(http.ListenAndServe(":8080", router))
    //host.GetStatsJson()
}


func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Welcome!")
}

func GetOSStats(w http.ResponseWriter, r *http.Request) {
    // js, err := host.GetStatsJson()   // This is what I would like to do
    // Ideally I would get the Marshalled json and err and return them. 
    // The object method works fine as I have tested it, I am however unable to call the object method here. 

    fmt.Println("getting json stats")
    host.GetStatsJson()  //This is were I get the server panic issue and the code breaks
    //I would like to access the method of the 'host' object defined in the main() method. 
    fmt.Fprintln(w, "GetOSStats!")
}

I would like to call the method of the object defined in the main() function inside the GetOSStats() method and then return the json output.

When I declare a global variable and then later on assign it in the main function, the GetOSStats() function is still accessing a nil struct.

When I declare the host obj in the main function and try to access it in the GetOSStats() funtion, it throws exception.

I think I have to pass the host obj by reference to the GetOSStats() function while calling it in the main but I am not sure about how to do that. I have tried looking up the documentations and the examples but I couldnot find anything that could help me.

Thanks in advance,

  • 写回答

1条回答 默认 最新

  • du1068 2019-03-26 22:36
    关注

    You're redeclaring the host variable locally (this is also called "shadowing")

    host := new(objects.HostOS)
    

    instead you should use an assignment operator

    host = new(objects.HostOS)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化