D
D
Dmitry Afonchenko2018-08-14 17:43:51
go
Dmitry Afonchenko, 2018-08-14 17:43:51

Why does Go throw Undefined for a struct declared in the same package?

Good afternoon, comrades! I'm doing a small project on Go, until recently it was going well, but for some reason in the following case it began to give an error:
The main.go file contains the following code:

package main

func main() {
  var m = Message{
    Type: "LOL",
  }
  println(m.Type)
}

The msg.go file contains the following code:
package main

// Message - test message
type Message struct {
  Type string
}

When running the command
go run main.go

Gives an error message:
.\main.go:4:10: undefined: Message

Why is that? Previously, everything worked fine in this case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2018-08-14
@Indermove

Because you are only running one main.go file. Either run all *.go, or write the names of 2 files - go run main.go msg.go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question