You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Golang同一代码在不同文件夹编译,一个正常一个报找不到包"."错误

Fixing the "cannot find package "."" Error in Your Second Go Project

Hey there, let's break down why your second Go project is hitting that error while the first one works perfectly—even though the code's identical. The issue almost always boils down to how Go handles package discovery, specifically with Go modules or the old GOPATH workspace setup.

Why This Happens

Go needs clear context to recognize your code as a valid package. Here's what’s likely going on:

  • Your first project folder is either inside your GOPATH/src directory (the legacy workspace mode) or has already been initialized as a Go module (it has a go.mod file).
  • Your second project folder is neither in GOPATH/src nor has a go.mod file, so Go can’t resolve the local package correctly.

Step-by-Step Fix (Modern Go Module Approach)

This is the recommended method for current Go versions:

  1. Navigate to your second project directory in your terminal:
    cd E:/Clouds/OneDrive/CX/GoLang/test
    
  2. Initialize a Go module for the project. You can use any name here (for local projects, even something simple like test works):
    go mod init test
    
    This creates a go.mod file in the folder, telling Go this is a standalone, valid module.
  3. Run your build command again:
    c:/go/bin/go.exe build -i E:/Clouds/OneDrive/CX/GoLang/test
    
    This should compile successfully, just like your first project.

Alternative: Legacy GOPATH Mode

If you prefer sticking to the older workspace system:

  • Run go env GOPATH to check your current GOPATH directory.
  • Move your second project into the src subfolder of that GOPATH. For example, if your GOPATH is E:/Clouds/OneDrive/CX/GoLang, your project should live at E:/Clouds/OneDrive/CX/GoLang/src/test. No go.mod file is needed here.

Quick Edge Case Checks

If the above doesn’t work, verify:

  • Your OneDrive folder has finished syncing (partial syncs can cause hidden file access issues).
  • The folder has write permissions (Go needs to create temporary files during compilation).

内容的提问来源于stack exchange,提问作者Jonathan Hecl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:14:59