Home >Backend Development >Golang >\'Import and Not Used\' Error: Why am I getting this and how can I fix it?
You're facing an "imported and not used" error while attempting to import a package called "api" from the file path "./api" in your main.go file.
The error message originates from the fact that the compiler requires actual utilization of imported packages within your source code. Although you have imported the "api" package, it's not being utilized in your code.
To resolve this, you'll need to either use elements from the "api" package or remove the import statement. For example, you could utilize the object "api" as follows:
<code class="go">v := api.Something</code>
Alternatively, if you don't intend to utilize any elements from the "api" package, you can remove the import statement altogether.
In your specific case, you're experiencing an additional issue where you're overwriting the imported "api" package by declaring a variable also named "api." This is causing a conflict for the compiler, which is unable to differentiate between the imported package and the variable you've defined.
To solve this and successfully use the "api" package, you can either:
Alias the import using the following syntax:
<code class="go">import ( // others here api_package "./api" )</code>
Additionally, it's recommended to import packages using the GOPATH rather than using relative paths as shown in your code.
The above is the detailed content of \'Import and Not Used\' Error: Why am I getting this and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!