是什么导致“紧急:运行时错误:无效的内存地址或nil指针取消引用”? [关闭]

I'm having a problem with my project.

getting the error:

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

What am I doing wrong?

package A

package a

import (
    "fmt"

    "github.com/lathrel/test/b"

)

type (
    // App ...
    App b.App
)

// Fine is a fine :) but was fine :/
func (app *App) Fine(str string) string {
    fmt.Println(str)
    return ""
}

package B

package b

// App is a test
type App struct {
    fine Controller
}

// Controller is a test
type Controller interface {
    Fine(str string) string
}

// InitB inits B :)
func InitB() {
    app := App{}

    app.fine.Fine("hi")
}

main.go

package main

import (
    "github.com/lathrel/test/b"
)

func main() {
    b.InitB()
}

Looking at package b:

// InitB inits B :)
func InitB() {
    app := App{}

    app.fine.Fine("hi")
}

You could see that you initalized a new App{} with a new Controller but you didn't fill in the Interface inside the Controller. An interface is something that expects a certain method from a type.

Here's a quick snippet that would solve it:

type NotFine struct{}

// Fine is a method of NotFine (type struct)
func (NotFine) Fine(str string) string {
    fmt.Println(str)
    return ""
}

// InitB, ideally, would take in the interface itself,
// meaning, the function becomes InitB(c Controller).
// You then initiate `app := App{fine: c}`.
// This is implemented in https://play.golang.org/p/ZfEqdr8zNG-
func InitB() {
    notfine := NotFine{}
    app := App{
        fine: notfine,
    }

    app.fine.Fine("hi")
}

Playground link: https://play.golang.org/p/ZfEqdr8zNG-