help: go build 失败,提示 usage: compile [options] file.go...

39 天前
 codists

一、说明

GOROOT=D:\apps\go1.18
GOPATH=D:\GoProject 

二、问题描述

在 D:\GoProject\src\power\project_name\cmd\listener> 目录下执行 go build 命令(说明:project 是 项目名称):

\GoProject\src\power\project\cmd\listener>go build .\main.go

结果编译失败,提示信息如下:

# auto-task/task/create                        
usage: compile [options] file.go...                  
  -% int                                             
        debug non-static initializers                
  -+    compiling runtime                            
  -B    disable bounds checking                      
  -C    disable printing of columns in error messages
  -D path
        set relative path for local imports
  -E    debug symbol export
  -G    accept generic code (default 3)
  -I directory
        add directory to import search path
  -K    debug missing line numbers
  -L    show full file names in error messages
  -N    disable optimizations
  -S    print assembly listing
  -V    print version and exit
  -W    debug parse tree after type checking
  -asan
        build code compatible with C/C++ address sanitizer
  -asmhdr file
        write assembly header to file
  -bench file
        append benchmark times to file
  -blockprofile file
        write block profile to file
  -buildid id
        record id as the build id in the export metadata
  -c int
        concurrency during compilation (1 means no concurrency) (default 1)
  -clobberdead
        clobber dead stack slots (for debugging)
  -clobberdeadreg
        clobber dead registers (for debugging)
  -complete
        compiling complete package (no C or assembly)
  -cpuprofile file
        write cpu profile to file
  -d value
        enable debugging settings; try -d help
  -dwarf
        generate DWARF symbols (default true)
  -dwarfbasentries
        use base address selection entries in DWARF (default true)
  -dwarflocationlists
        add location lists to DWARF in optimized mode (default true)
  -dynlink
        support references to Go symbols defined in other shared libraries
  -e    no limit on number of errors reported
  -embedcfg file
        read go:embed configuration from file
  -gendwarfinl int
        generate DWARF inline info records (default 2)
  -goversion string
        required version of the runtime
  -h    halt on error
  -importcfg file
        read import configuration from file
  -importmap definition
        add definition of the form source=actual to import map
  -installsuffix suffix
        set pkg directory suffix
  -j    debug runtime-initialized variables
  -json string
        version,file for JSON compiler/optimizer detail output
  -l    disable inlining
  -lang string
        Go language version source code expects
  -linkobj file
        write linker-specific object to file
  -linkshared
        generate code that will be linked against Go shared libraries
  -live
        debug liveness analysis
  -m    print optimization decisions
  -memprofile file
        write memory profile to file
  -memprofilerate rate
        set runtime.MemProfileRate to rate
  -msan
        build code compatible with C/C++ memory sanitizer
  -mutexprofile file
        write mutex profile to file
  -nolocalimports
        reject local (relative) imports
  -o file
        write output to file
  -p path
        set expected package import path
  -pack
        write to file.a instead of file.o
  -r    debug generated wrappers
  -race
        enable race detector
  -shared
        generate code that can be linked into a shared library
  -smallframes
        reduce the size limit for stack allocated objects
  -spectre list
        enable spectre mitigations in list (all, index, ret)
  -std
        compiling standard library
  -symabis file
        read symbol ABIs from file
  -t    enable tracing for debugging the compiler
  -traceprofile file
        write an execution trace to file
  -trimpath prefix
        remove prefix from recorded source file paths
  -v    increase debug verbosity
  -w    debug type checking
  -wb
        enable write barrier (default true)

说明:(1)错误提示中第一行的 auto-task/task/create 是项目的目录。

二、尝试的解决方法

1.修改 project 上一级目录名称,如 power 修改为 power1,结果可以正常编译。  
2.将 project 移动到 GoProject 之外的目录,结果可以正常编译。  

请教各位大佬,为什么用 power 这个名字就不行,可能是因为什么原因造成的?

886 次点击
所在节点    Go 编程语言
1 条回复
body007
38 天前
第二个就是答案,自从有了 go mod 之后,不要把项目放到 gopath 目录下,gopath 在 go mod 之后就是个缓存目录,里面的文件也尽量不要编辑。

我估计你的 go mod 文件 第一行有包含 .../power/project/... ,go 编译时会查找 gopath 目录下的代码也会查找 go.mod 文件开始的子目录路径,而你把项目放到 gopath 目录下,估计引起了冲突。把 power 改为 power1 后,如果你的 go.mod 文件没有同步更改,那么 go 在查找路径时不会冲突,所以第一个也能编译(这个是我推测的)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1041884

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX