dongqie2010 2016-03-29 16:28
浏览 408
已采纳

无法导入自定义go模块

Here is my main file (server.go):

package main

import (
    "net/http"
    "routes"
)

func main() {
    http.HandleFunc("/", routes.Handler)
    http.ListenAndServe(":8000", nil)
}

My routes module is in the same directory:

package routes

func Handler(w http.ResponseWriter, r *http.Request) {
    // stuff...
}

When I run go run server.go I get this error:

server.go:6:5: cannot find package "routes" in any of:
    /usr/local/Cellar/go/1.6/libexec/src/routes (from $GOROOT)
    ~/server/src/routes (from $GOPATH)

When I place the code in routes.go into my server.go file, it runs fine. I cannot import the module. I have tried setting the $GOPATH variable to my current directory, I've tried rearranging my project directory to mimic the one here. I'm running out of options. It is strange that a language with such wide adoption has such poor documentation on how to do something that is relatively easy in almost every other language. Please help me figure out what I'm doing wrong.

UPDATE:

This is the output of go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/server"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.6/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.6/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
  • 写回答

2条回答 默认 最新

  • douhao6271 2016-03-29 17:37
    关注

    Move the routes package to /Users/me/server/src/routes and you should be good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况