Import Package and Type
Question:
In the following project structure:
src |-->config |--> config.go |-->otherPackage |--> otherFile.go |-->main.go
There is a type defined in config.go that needs to be used in otherFile.go. However, attempting to import it using the following statement results in errors:
import ( "fmt" "math" "../config" )
The specific errors are:
Even though the type Config is used in a function declaration.
What is the underlying problem?
Answer:
The issue stems from trying to import only a specific type from a package. In Go, it is not possible to import specific elements from a package. When importing a package, you are essentially importing its entire contents.
In this case, since the package where the Config type is defined is named config, importing it will make its types and other identifiers available under the config namespace. For instance, to use Config, it must be referenced as config.Config.
Therefore, to resolve the problem, one of the following solutions can be employed:
위 내용은 Go에서 특정 유형을 가져올 때 "가져왔지만 사용되지 않음" 및 "정의되지 않음" 오류가 발생하는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!