Go服务器不显示内部导入的智能感知。

huangapple go评论93阅读模式
英文:

Go server not showing IntelliSense of internal imports

问题

我正在使用golang和GoLand(Jetbrains)IDE在UBUNTU上进行开发。我的Go路径是/usr/local/go,我的项目目录位于/Home/GoLandProjects,其中包含bin,src,pkg目录和我的项目目录。

我的GOPATH在.bashrc文件中设置如下:

export GOPATH="$HOME/GolandProjects"
export PATH="$PATH:$GOPATH/bin:/usr/local/go/bin"

一切都很好,如果我运行我的代码,它完全正常工作,但是我的导入部分出现问题,没有显示智能提示建议。
项目内部的导入不起作用。

import (
    "context" //工作正常
    "errors" //工作正常
    "go.mongodb.org/mongo-driver/bson" //工作正常
    "go.mongodb.org/mongo-driver/bson/primitive" //工作正常
    "go.mongodb.org/mongo-driver/mongo" //工作正常

    "my-server/helpers" //不工作-没有显示智能提示,显示为红色
    "my-server/models"  //不工作-没有显示智能提示,显示为红色

    "time" //工作正常
)

代码可以运行,但是没有内部导入的智能提示。

我的go.mod文件如下:

module my-server

// +heroku goVersion go1.16
go 1.16

require (
    cloud.google.com/go v0.91.1 // indirect
    cloud.google.com/go/firestore v1.5.0 // indirect
    cloud.google.com/go/storage v1.16.0 // indirect
    firebase.google.com/go v3.13.0+incompatible
    github.com/aead/chacha20poly1305 v0.0.0-20170617001512-233f39982aeb // indirect
    github.com/gin-contrib/cors v1.3.1
    github.com/gin-gonic/gin v1.7.3
    github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
    github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
    github.com/gorilla/websocket v1.4.2 // indirect
    github.com/mikunalpha/goas v1.6.0 // indirect
    github.com/o1egl/paseto v1.0.0
    go.mongodb.org/mongo-driver v1.7.1
    golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
    golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5
    golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
    golang.org/x/text v0.3.7 // indirect
    google.golang.org/api v0.54.0
    google.golang.org/genproto v0.0.0-20210813162853-db860fec028c // indirect
    google.golang.org/grpc v1.40.0 // indirect
)

在helpers目录中:

./helpers:
total 20
drwxrwxr-x 2 zainkhan zainkhan 4096 Sep  3 12:45 .
drwxrwxr-x 9 zainkhan zainkhan 4096 Sep  4 12:40 ..
-rw-rw-r-- 1 zainkhan zainkhan   16 Sep  2 16:42 firebase.go
-rw-rw-r-- 1 zainkhan zainkhan  854 Sep  2 16:42 mongo.go
-rw-rw-r-- 1 zainkhan zainkhan 3138 Sep  3 12:45 mongo_utils.go

以上是您要翻译的内容。

英文:

I'm using golang and GoLand(Jetbrains)IDE for development on UBUNTU. My Go path is /usr/local/go and my Project Directory is at /Home/GoLandProjects with bin, src, pkg directories and my project directory.

go version go1.16.7 linux/amd64

My GOPATH is set like this in .bashrc:

export GOPATH="$HOME/GolandProjects"
export PATH="$PATH:$GOPATH/bin:/usr/local/go/bin"

Everything is good, If I run my code its totally works fine but my import is making issues, not showing Intellisence-suggestions.
Imports from within the project are not working.

import (
"context" //woking
"errors" //woking
"go.mongodb.org/mongo-driver/bson" //woking
"go.mongodb.org/mongo-driver/bson/primitive" //woking
"go.mongodb.org/mongo-driver/mongo" //woking
"my-server/helpers" //not woking - not showing intelliSense and in Red Color 
"my-server/models"  //not woking - not showing intelliSense and in Red Color 
"time" //woking
)

Code is running but no intelliSense of internal imports.

My go.mod file:

module my-server
// +heroku goVersion go1.16
go 1.16
require (
cloud.google.com/go v0.91.1 // indirect
cloud.google.com/go/firestore v1.5.0 // indirect
cloud.google.com/go/storage v1.16.0 // indirect
firebase.google.com/go v3.13.0+incompatible
github.com/aead/chacha20poly1305 v0.0.0-20170617001512-233f39982aeb // indirect
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.3
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/mikunalpha/goas v1.6.0 // indirect
github.com/o1egl/paseto v1.0.0
go.mongodb.org/mongo-driver v1.7.1
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/api v0.54.0
google.golang.org/genproto v0.0.0-20210813162853-db860fec028c // indirect
google.golang.org/grpc v1.40.0 // indirect
)

ls -alR

drwxrwxr-x 9 zainkhan zainkhan  4096 Sep  4 12:40 .
drwxrwxr-x 8 zainkhan zainkhan  4096 Sep  2 16:45 ..
-rw-rw-r-- 1 zainkhan zainkhan  2373 Sep  2 16:42 admin-pk.json
drwxrwxr-x 2 zainkhan zainkhan  4096 Sep  4 17:08 controllers
-rw-rw-r-- 1 zainkhan zainkhan  2383 Sep  2 16:42 credentials.json
-rw-rw-r-- 1 zainkhan zainkhan   123 Sep  2 16:42 Dockerfile
drwxrwxr-x 8 zainkhan zainkhan  4096 Sep  4 17:08 .git
drwxrwxr-x 3 zainkhan zainkhan  4096 Sep  2 16:42 .github
-rw-rw-r-- 1 zainkhan zainkhan     6 Sep  2 16:42 .gitignore
-rw-rw-r-- 1 zainkhan zainkhan  1078 Sep  4 12:40 go.mod
-rw-rw-r-- 1 zainkhan zainkhan 70236 Sep  2 16:45 go.sum
drwxrwxr-x 2 zainkhan zainkhan  4096 Sep  3 12:45 helpers
drwxrwxr-x 3 zainkhan zainkhan  4096 Sep  4 17:14 .idea
-rw-rw-r-- 1 zainkhan zainkhan  1403 Sep  4 12:40 main.go
drwxrwxr-x 3 zainkhan zainkhan  4096 Sep  4 16:28 models
drwxrwxr-x 2 zainkhan zainkhan  4096 Sep  4 17:08 services

in helpers dir

./helpers:
total 20
drwxrwxr-x 2 zainkhan zainkhan 4096 Sep  3 12:45 .
drwxrwxr-x 9 zainkhan zainkhan 4096 Sep  4 12:40 ..
-rw-rw-r-- 1 zainkhan zainkhan   16 Sep  2 16:42 firebase.go
-rw-rw-r-- 1 zainkhan zainkhan  854 Sep  2 16:42 mongo.go
-rw-rw-r-- 1 zainkhan zainkhan 3138 Sep  3 12:45 mongo_utils.go

Go服务器不显示内部导入的智能感知。
Go服务器不显示内部导入的智能感知。
Go服务器不显示内部导入的智能感知。

答案1

得分: 1

GOPATH与go mod不同。
修改你的go.mod文件,使用replace来定位你的本地包。

Go服务器不显示内部导入的智能感知。

> 将package替换为path/to/package

英文:

GOPATH is not same as go mod.
Modify your go.mod file to locate your local package using replace.

Go服务器不显示内部导入的智能感知。

> replace package => path/to/package

huangapple
  • 本文由 发表于 2021年9月4日 14:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/69052927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定