dtzd65908 2017-11-08 03:59
浏览 722
已采纳

Golang错误:找不到包命令

I'm getting this error when running a .go file.

./instance.go: line 1: package: command not found
./instance.go: line 3: syntax error near unexpected token `newline'
./instance.go: line 3: `import ('

So far I've seen that the error normally means that GOPATH is not set, however, echo "$GOPATH" outputs /root/go and my path is currently /bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/go/bin:/root/go/bin:/bin:/root/go/bin .

Here's the first few lines of instance.go:

package webrun

import (
        "context"
        "crypto/hmac"
        "crypto/rand"
  • 写回答

2条回答 默认 最新

  • douqujin2767 2017-11-08 04:29
    关注

    These are not Go errors.

    However, I was able to reproduce your errors by trying to execute a go source file:

    $ chmod +x in.go
    $ ./in.go
    ./in.go: line 1: package: command not found
    ./in.go: line 3: syntax error near unexpected token `newline'
    ./in.go: line 3: `import ('
    

    You can't do this with Go, since it is not interpreted. You need to compile your code to an executable before you can run it.

    You can read the help for how to compile and run a go program here.

    In your case,

    go run instance.go
    

    will try to compile and run the source file you're executing, but as another commenter points out, code that is not in the main package is not designed to be executed directly.

    If this is your code, you need to write it inside package main, and if it is not, then you're probably trying to execute a library (which means it's not clear to me what you're trying to do).

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

报告相同问题?

悬赏问题

  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader