无法使用sudo命令运行go程序

时间:2019-04-25 06:42:13

标签: go amazon-s3

我是新来的编程语言。

我正在使用下面的程序将my-s3zipper.go程序打包。

"github.com/AdRoll/goamz/aws"
"github.com/AdRoll/goamz/s3"

当我与本地用户一起运行go程序时,它运行良好。下面是go run命令。

go run my-s3zipper.go
Running on port 80

当我使用sudo运行go程序时,它没有运行并引发错误。下面是使用sudo的go run命令。

sudo go run my-s3zipper.go 



my-s3zipper.go:19:5: cannot find package "github.com/AdRoll/goamz/aws" in any of:
        /usr/lib/golang/src/github.com/AdRoll/goamz/aws (from $GOROOT)
        /root/go/src/github.com/AdRoll/goamz/aws (from $GOPATH)
my-s3zipper.go:20:5: cannot find package "github.com/AdRoll/goamz/s3" in any of:
        /usr/lib/golang/src/github.com/AdRoll/goamz/s3 (from $GOROOT)
        /root/go/src/github.com/AdRoll/goamz/s3 (from $GOPATH)
my-s3zipper.go:21:5: cannot find package "github.com/garyburd/redigo/redis" in any of:
        /usr/lib/golang/src/github.com/garyburd/redigo/redis (from $GOROOT)
        /root/go/src/github.com/garyburd/redigo/redis (from $GOPATH)

有人能帮助我解决这个问题吗?

谢谢

2 个答案:

答案 0 :(得分:1)

这可能有效

sudo -E go run my-s3zipper.go

在sudo手册页中

  

-E'-E(保留环境)选项向安全策略指示用户希望保留其现有环境   变量。如果-E选项为,则安全策略可能会返回错误。   指定,并且用户无权保留   环境。

答案 1 :(得分:-1)

尝试:

A.class{

    @HeaderParam("USER") 
    private String userid;

    public void setUserId(String userid){
     this.userid=userid;
    }
    public String getUserId(String userid){
     return userid;
    }
}

testMethod(){
    @Spy
    A a;

    a.setUserId("Any String"); 

    a.getUserId(); //it equals "Any String"
}

sudo GOPATH="$GOPATH:/your/home/go/path" go run my-s3zipper.go

原因可能是用户和root用户的GOPATH变量不同。

您可以通过在登录用户时执行su GOPATH="$GOPATH:/your/home/go/path" go run my-s3zipper.go 来检测/your/home/go/path

相关问题