V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
codists
V2EX  ›  Go 编程语言

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

  •  
  •   codists ·
    codists · 191 天前 · 1086 次点击
    这是一个创建于 191 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一、说明

    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 这个名字就不行,可能是因为什么原因造成的?

    1 条回复    2024-05-19 09:56:07 +08:00
    body007
        1
    body007  
       190 天前
    第二个就是答案,自从有了 go mod 之后,不要把项目放到 gopath 目录下,gopath 在 go mod 之后就是个缓存目录,里面的文件也尽量不要编辑。

    我估计你的 go mod 文件 第一行有包含 .../power/project/... ,go 编译时会查找 gopath 目录下的代码也会查找 go.mod 文件开始的子目录路径,而你把项目放到 gopath 目录下,估计引起了冲突。把 power 改为 power1 后,如果你的 go.mod 文件没有同步更改,那么 go 在查找路径时不会冲突,所以第一个也能编译(这个是我推测的)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2412 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:03 · PVG 00:03 · LAX 08:03 · JFK 11:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.