dongtang1966 2018-05-26 19:04
浏览 54
已采纳

“指向接口的指针,而不是接口”

I am learning Go and decided to rewrite a MQTT orchestrator which I originally wrote in Python. The very basic part works fine:

package main

import (
    "fmt"
    "time"
    "os"

    MQTT "github.com/eclipse/paho.mqtt.golang"
    log "github.com/sirupsen/logrus"
)

// definitions for a switch

type Switch struct {
    topic string
    state int
}

func allEvents(client MQTT.Client, msg MQTT.Message) {
    log.WithFields(log.Fields{"topic": msg.Topic(), "payload": fmt.Sprintf("%s", msg.Payload())}).Info()
}


func initMQTT() MQTT.Client {
    opts := MQTT.NewClientOptions()
    opts.AddBroker("tcp://mqtt.example.com:1883")
    opts.SetClientID("go-dispatcher")
    opts.SetCleanSession(true)
    client := MQTT.NewClient(opts)
    if token := client.Connect(); token.Wait() && token.Error() != nil {
        panic(token.Error())
    }
    log.Info("connected to MQTT broker")
    return client
}

func main() {
    // this is that part I want to modify later in the question
    c := initMQTT()
    if token := c.Subscribe("#", 0, allEvents); token.Wait() && token.Error() != nil {
        fmt.Println(token.Error())
        os.Exit(1)
    }

    time.Sleep(100000 * time.Hour)
}

Having used pointers in a distant past with C, I wanted to modify the program to pass, in the initialization part, the client by reference (more as a learning experience, the first code looks better to me)

package main

import (
    "fmt"
    "time"
    "os"

    MQTT "github.com/eclipse/paho.mqtt.golang"
    log "github.com/sirupsen/logrus"
)

// definitions for a switch

type Switch struct {
    topic string
    state int
}

func allEvents(client MQTT.Client, msg MQTT.Message) {
    log.WithFields(log.Fields{"topic": msg.Topic(), "payload": fmt.Sprintf("%s", msg.Payload())}).Info()
}

// this time we get a pointer and do not return anything
func initMQTT(client *MQTT.Client) {
    opts := MQTT.NewClientOptions()
    opts.AddBroker("tcp://mqtt.example.com:1883")
    opts.SetClientID("go-dispatcher")
    opts.SetCleanSession(true)
    client = MQTT.NewClient(opts)
    if token := *client.Connect(); token.Wait() && token.Error() != nil {
        panic(token.Error())
    }
    log.Info("connected to MQTT broker")
}

func main() {
    // the client is defined in main()
    var c MQTT.Client
    // and passed by reference so that the function modifies it
    initMQTT(&c)
    if token := c.Subscribe("#", 0, allEvents); token.Wait() && token.Error() != nil {
        fmt.Println(token.Error())
        os.Exit(1)
    }

    time.Sleep(100000 * time.Hour)
}

It fails to compile with

# command-line-arguments
.\main.go:29:9: cannot use mqtt.NewClient(opts) (type mqtt.Client) as type *mqtt.Client in assignment:
    *mqtt.Client is pointer to interface, not interface
.\main.go:30:21: client.Connect undefined (type *mqtt.Client is pointer to interface, not interface)

Following advice form another question, I tried to remove & and * (blindly to start with, to tell the truth), and get a runtime error

time="2018-05-26T21:02:20+02:00" level=info msg="connected to MQTT broker"
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x604486]

goroutine 1 [running]:
main.main()
    D:/Seafile/dev/Go/src/perso/domotique.dispatcher/main.go:39 +0x36

I though that since I defined c, I could just pass it as a reference but apparently the C-way is not the right one here? (it is usually in the examples I read)

  • 写回答

1条回答 默认 最新

  • douhua1760 2018-05-26 19:27
    关注

    This is a common misunderstanding. Because you've defined c as a value rather than a pointer type, you can't modify it without re-defining it.

    The fact that you've passed its memory address does not enable you to modify it. This is different from a number of other languages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog