Importing Packages and Types
In Go, a common issue arises when attempting to import a type from a different package. This problem is highlighted by the following code structure:
src |-->config |--> config.go |-->otherPackage |--> otherFile.go |-->main.go
The goal is to use a type declared in config.go within the otherFile.go file. However, importing config within otherFile.go leads to errors such as "imported and not used" and "undefined: Config."
Go does not support importing specific types from a package. Instead, you must import the entire package, thus qualifying any type references with the package name, like so:
import ( "fmt" "math" "./config" )
Using this import statement, you can reference the type Config from config.go using the fully qualified name config.Config. Alternatively, to prevent shadowing, you can:
위 내용은 Go에서 다양한 패키지의 유형을 사용하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!