使用main.go在其他程序包中声明的结构

I have a structure declared in main.go as shown below:

type Organization struct {
    ID    string `json:"id"`
    Count int    `json:"count"` //node count
}

I have a package named configuration and have a function like this:

func foo(org main.Organization) {

}

The issue is I am not able to access main.Organization. Is there any way to access struct declared in main.go in another package?

It's not possible to import package main except in certain rare circumstances.

I found this explanation on a mailing list dated back last year.

However importing a main package in another main package works. To summarize:

1) importing a main package in a external test is supported
2) importing a main package in a main package is supported
3) importing a main package in a non main package is not supported

This seems reasonable to me, however AFAIK it is not documented.

From the language spec

A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.

(emphasis mine in both quotes)

You can't import the main package from other packages in go(except in certain situations, such as a test case).

Instead create a new directory e.g. named mytypes , In this folder create the file types.go which would look like:

package mytypes
type Organization struct {
    ID    string `json:"id"`
    Count int    `json:"count"` //node count
}

Wherever you want to use this struct, such as in main an , you import "mytypes" and use the Organization struct as mytypes.Organization

Alternatively, you move the Organization struct to your configuration package and use configuration.Organization in your main.