can't find struct in a same package


(Cosmo) #1

suddenly, vscode cannot get access struct in the same package,but different files.

the error showed undefined struct

Previously vscode was able to instantiate struct across files(vscode+go1.12.4)

the sample code like this:

file1 a.go

package main

import "fmt"

type su struct {
	Sfd string
	Sf  int
}

func (sed *su) do() {
	fmt.Println(sed.Sfd + string(sed.Sf))
}


file2 main.go

package main

import "fmt"

func main() {
	d := su{}
	fmt.Println(d)
}

these two files are in the same package

first of all, i can run it perfect in playground

have some one met the same problem?


(Norbert Melzer) #2

Don’t split the main package.


(Jaron R. Swab) #3

First off welcome to the forum!
I hope you gain as much value here as I have over the past few months.

From the Go Language Specification:

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.

func main() { … }

Program execution begins by initializing the main package and then invoking the function main . When that function invocation returns, the program exits. It does not wait for other (non- main ) goroutines to complete.

Notice the first sentence here says single.

Golang needs one main for it to start up, run, and close the program.

Main is also not imported anywhere so even if you wanted to make another Go file at the main package scope there is no way to import the code.

If you need to have a.go in its own file then create a package with a new name, export the struct and function, then import that new package into main.go.

Otherwise, add the struct and method to main.go and remove a.go.

Single File Example:

package main

import "fmt"

func main() {
	d := su{"test string", 2}
	d.do()
}

type su struct {
	Sfd string
	Sf  int
}

func (sed *su) do() {
	fmt.Printf("Sfd: %s\nSf: %d", sed.Sfd, sed.Sf)
}

Playground Link


(Ignacio Gómez) #4

Technically, there’s nothing wrong with having several files in the main package and it should work without issues (e.g. dockerd), as it does in your playground example. I’ve also checked it locally in vscode and there’s no problem either, so I’m guessing you have something misconfigured at your install.


(Prithvipal Singh) #5

@iegomez I tried to split the main package but I am also getting compilation error(undefined su). I have referred the dockerd link you have shared and there no special handling in that link. I am wondering how it is working for you(and in dockerd) but not working in our local environment. Can you please explain is there any additional configuration we need?


(Cosmo) #6

thanks bro for your answer. i have been playing golang for almost one year and i feel it’s too late to meet it. recently, i found this problem, and i think it’s interesting, but i dont understand why, so i seek answer in this big family


(Cosmo) #7

i guess that’s a bug of vscode


(Ignacio Gómez) #8

My best guess would be that it’s related to configuration of GOPATH or go mod at vscode in some way.

I mean, the original example builds and runs without a problem, you can try it yourself with go build from a terminal. I copied the exact same files an threw them in a test dir and all I had to do was go mod init (as I have modules enabled by default), go build and ./test to get it to run, either from a separate terminal or from one opened at vscode. I also don’t have anything special installed except the Go language support extension, and I do get hints and everything is properly recognized when coding, e.g., if I type d. at file2.go, vscode suggests the do method and both Sfd and Sf for autocompletion.


(Cosmo) #9

i set the gopath and goroot as environment variables. and i run it in another powershell. the problem is still . it is strange that the vscode always can recognized when i code the struct. but when i go run/build it the problem arises.


(Johan Dahl) #10

Do you run it and build it by giving both files as argument? Like

go run file1.go file2.go

(system) closed #11

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.