doumeng3080 2016-02-24 10:31
浏览 36
已采纳

go中的结构和使用指针

I am trying my hand on my first go program which is supposed to be a very simple IRC bot.

I have the part of the connections etc done but I am confused with structs and pointers such. The structs are new to me coming from languages that use classes.

I have this struct and the constructor for it:

type Bot struct {
    server  string
    port    string
    nick    string
    channel string
    pass    string
    conn    net.Conn
}

// NewBot main config
func NewBot() *Bot {
    return &Bot{
        server:  "irc.twitch.tv",
        port:    "6667",
        nick:    "username",
        channel: "#channel",
        pass:    "password123",
        conn:    nil,
    }
}

My connect() method looks like this

func (bot *Bot) Connect() (conn net.Conn, err error) {
    ircbot := NewBot()
    conn, err = net.Dial("tcp", bot.server+":"+bot.port)
    // irc connection...
    return bot.conn, nil
}

Everything of that works fine the problem I am having is with another method to my struct named Message. It's just supposed to send a message. Looks like this:

// Message to send a message
func (bot *Bot) Message(message string) {
    if message == "" {
        return
    }
    fmt.Printf("Bot: " + message + "
")
    fmt.Fprintf(bot.conn, "PRIVMSG "+bot.channel+" :"+message+"
")
}

everytime when I then try to use this function I get this error and the program crashes

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x463d73]

I'm unsure about how to use the & and * signs correctly to achieve what I want to do.

I thought a goroutine is something that is for multithreading and is done by saying "go [do something]" but I don't use that anywhere.

Edit: Solution

Thanks I found the solution! For those interested: I created a new instance of Bot in the place where I called the Message() function which resulted in an empty conn.

This was the important bit, I stupidly didn't post here. handle() wasn't even a method of Bot which is even more stupid of me.

func handle(line string) {
    ircbot := NewBot();
    // get username, message etc...
    ircbot .CmdInterpreter(username[1], usermessage)
}

and this is the correct way:

func (bot *Bot) handle(line string) {
    // get username, message etc...
    bot.CmdInterpreter(username[1], usermessage)
}
  • 写回答

1条回答 默认 最新

  • dongtun2459 2016-02-24 10:52
    关注

    The issue here seems to be with the Connect method, and not really with pointers in particular:

    func (bot *Bot) Connect() (conn net.Conn, err error) {
        ircbot := NewBot()
        conn, err = net.Dial("tcp", bot.server+":"+bot.port)
        // irc connection...
        return bot.conn, nil
    }
    

    The method is defined to be on (a pointer to the) Bot struct, but it creates a new Bot, on this line:

    ircbot := NewBot()
    

    and then proceeds to use the one the method is defined on (called bot, not ircbot). If you choose to keep it so that there are separate NewBot and Connect functions (which is fine), then you should change it so that Connect actually uses an instantiated *Bot:

    func (bot *Bot) Connect() (conn net.Conn, err error) {
        conn, err = net.Dial("tcp", bot.server+":"+bot.port)
        // irc connection...
        return bot.conn, nil
    }
    

    and call it with something like this:

    bot := NewBot()
    conn, err := bot.Connect()
    

    The error,

    panic: runtime error: invalid memory address or nil pointer dereference
    

    is probably because you are accessing bot in the Connect method (e.g. in bot.server) but it has not been defined, and the pointer is nil.

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

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题