在GO语言中遇到赋值问题。

huangapple go评论125阅读模式
英文:

Trouble assigning value in GO

问题

我正在尝试理解为什么会出现错误:invalid memory address or nil pointer dereference

假设我有以下代码(这不是真实的代码,但类似):

type (
    Cat struct {
        Name string
    }

    Dog struct {
        Name string
    }

    Pets struct {
        c *Cat
        d *Dog
    }
)

func (c *Cat) Rename(d Dog) string {
    err := SomeErrorChecks()
    name := strings.TrimSpace(d.Name)
    c.Name = name // 在这一行出现错误

    return err
}

func (p *Pets) SomeFunction() string {
    p.d = &Dog{}
    p.c = &Cat{}

    p.d.Name = "Foo"

    err := p.c.Rename(*p.d)

    return err
}

func main() {
    // 一些代码

    err := SomeFunction()

    // 一些与错误处理无关的代码
}

我已经检查过strings.TrimSpace(d.Name)返回了期望的字符串。我也确认了c.Name的类型是字符串。那么,为什么我不能将name赋值给c.Name,如果它们都是相同的类型呢?

英文:

I'm trying to understand why I'm getting the error: invalid memory address or nil pointer dereference

Let's say I have this code (this is not the real one but similar):

type (

	Cat struct {
    	Name string 
    }

	Dog struct {
    	Name string 
    }

    Pets struct{
        c *Cat
        d *Dog
    }
)

func (c *Cat) Rename(d Dog) string {
     err := SomeErrorChecks()
     name := strings.TrimSpace(d.Name)
     c.Name = name // In this line is where I get the error

     return err         
}

func (p *Pets) SomeFunction() string{
    p.d = &Dog{} 
    p.c = &Cat{}

    p.d.Name = " Foo "

    err := p.c.Rename(*p.d)

    return err
}

func main(){
    //some code

    err := SomeFunction()

    //some error handling not relevant
}

I've checked that strings.TrimSpace(d.Name) returns de desired string. And I've checked that the type of c.Name is string. So, why I can't assing name to c.Name if both are the same type?

答案1

得分: 1

我看不出代码有什么问题。也许只是代码风格和方法名的问题。为了使其工作,我添加了一些返回值,你需要进行调整,并删除了C++风格的注释。

请注意,由于p := &Pets{}的赋值,nil接收器的问题不是一个问题。

package main

import (
    "fmt"
    "strings"
)

type (
    Cat struct {
        Name string 
    }

    Dog struct {
        Name string 
    }

    Pets struct{
        c *Cat
        d *Dog
    }
)

func (c *Cat) Rename(d Dog) (string, error) {
     name := strings.TrimSpace(d.Name)
     c.Name = name
     return "", nil
}

func (p *Pets) SomeFunction() (err error){
    p.d = &Dog{} 
    p.c = &Cat{}

    p.d.Name = " Foo "

    p.c.Rename(*p.d)
    return nil
}

func main(){
    p := &Pets{}
    p.SomeFunction()
    fmt.Printf("%s\n", p.c.Name)
}

你可以在这里查看代码:https://play.golang.org/p/bavuUn9IpT

英文:

I see no problem with the code. Maybe just the code style and the method name. To make it work I added some return values that you should adjust and removed the C++ style comments.

Note that the problem with a nil receiver is not an issue because of the p := &Pets{} assignment.

https://play.golang.org/p/bavuUn9IpT

package main

import (
    "fmt"
    "strings"
)

type (

    Cat struct {
        Name string 
    }

    Dog struct {
        Name string 
    }

    Pets struct{
        c *Cat
        d *Dog
    }
)

func (c *Cat) Rename(d Dog) (string, error) {
     name := strings.TrimSpace(d.Name)
     c.Name = name
     return "", nil
}

func (p *Pets) SomeFunction() (err error){
    p.d = &Dog{} 
    p.c = &Cat{}

    p.d.Name = " Foo "

    p.c.Rename(*p.d)
    return nil
}

func main(){
    p := &Pets{}
    p.SomeFunction()
    fmt.Printf("%s\n", p.c.Name)
}

答案2

得分: 1

正如@JimB在他的评论中指出的那样:

> 你正在使用一个空接收器调用Rename

这意味着,由于你在c.Name = name处遇到错误,Rename方法内部的c接收器是空的。在调用Rename之前,你需要确保初始化c

示例

英文:

As @JimB already pointed out in his comment

> You are calling Rename with a nil receiver

which means that, since you're getting the error at c.Name = name, the c receiver inside the Rename method is nil. You need to make sure to initialize c before calling Rename.

Example

huangapple
  • 本文由 发表于 2017年5月18日 05:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/44035193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定