这段代码有什么问题,会抛出指针错误。

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

What is wrong with this following code which throw pointer error

问题

有人能告诉我这段代码有什么问题吗?

package main

import "fmt"

type Document struct{
    testString string
}

type Printer interface{
    Print(d *Document)
}

type Scanner interface{
    Scan(d *Document)
}

type MultiFunctionMachine struct{
    printer Printer
    scanner Scanner    
}

func (m *MultiFunctionMachine)Print(d *Document){        
    m.printer.Print(d)
}

func main(){
    doc:= Document{"test"}        
    multiMachine:= MultiFunctionMachine{}
    multiMachine.Print(&doc)
}

我无法弄清楚为什么它一直抛出以下错误。似乎指针有问题。

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1092b46]

goroutine 1 [running]:
main.(*MultiFunctionMachine).Print(...)
    /Users/dmml/Documents/golang/udemyGoCourses/designPatternInGo/solidDesignPrinciples/interfaceSegrationPrinciple/main.go:85
main.main()
    /Users/dmml/Documents/golang/udemyGoCourses/designPatternInGo/solidDesignPrinciples/interfaceSegrationPrinciple/main.go:94 +0x46
exit status 2
英文:

Could anyone tell me what's wrong with this code?

package main

import "fmt"

type Document struct{
	testString string
}

type Printer interface{
	Print(d *Document)
}

type Scanner interface{
	Scan(d *Document)
}

type MultiFunctionMachine struct{
	 printer Printer
	 scanner Scanner	
}

func (m *MultiFunctionMachine)Print(d *Document){		
	m.printer.Print(d)
}

func main(){
	doc:= Document{"test"}		
	multiMachine:= MultiFunctionMachine{}
	multiMachine.Print(&doc)
}

I couldn't figure out why it keeps throwing this following error. It seems like something is wrong with the pointers.

*panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1092b46]

     goroutine 1 [running]:
main.(*MultiFunctionMachine).Print(...)
	/Users/dmml/Documents/golang/udemyGoCourses/designPatternInGo/solidDesignPrinciples/interfaceSegrationPrinciple/main.go:85
main.main()
	/Users/dmml/Documents/golang/udemyGoCourses/designPatternInGo/solidDesignPrinciples/interfaceSegrationPrinciple/main.go:94 +0x46
exit status 2*

答案1

得分: 1

你的MultiFunctionMachine结构体需要使用具体的实现来初始化它所持有的Printer和Scanner接口。换句话说,你需要定义一个或两个类型(结构体或其他类型),它们实现了Scan(d *Document)和Print(d *Document)接口(即具有与这些接口相同签名的函数)。然后将这些具体类型分配给MultiFunctionMachine中的接口字段。只有这样,m:MultiFunctionMachine才能被使用。你之所以得到一个空指针解引用错误,是因为这些字段没有以这种方式初始化。

英文:

your MultiFunctionMachine struct needs to be initialized with concrete implementations for both the Printer and Scanner interfaces it holds. In other words, you need to define one or two types (structs or otherwise) that implement the Scan(d *Document) and Print(d *Document) interfaces (ie have functions with the same signature as these interfaces). Then assign these concrete types to the interface fields in MultiFunctionMachine. Only then m:MultiFunctionMachine can be used. You are getting a nil pointer dereference error because these fields were not initialized in this manner.

huangapple
  • 本文由 发表于 2021年9月12日 12:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/69148413.html
匿名

发表评论

匿名网友

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

确定