go: cannot find main module, but found .git config to create a module there, run: go mod

go: cannot find main module, but found .git/config to create a module there, run: go mod

This problem occurred in a monorepo opened in VSCode. The repo has many services placed in isolated directories with respective go.mod files.

I realized from various sources that the problem is from two places:

  1. Move the mono repo under $GOPATH/src.
  2. The ~/.zshrc or ~/.bashrc must be as follows:
    export GOPATH=$HOME/go/
    export GOROOT=/usr/local/go
    
    export PATH="$GOROOT/bin:$PATH"
    export PATH="$PATH:$GOPATH/bin"
    

Do not add any other variable that affects gopls such as GO111MODULE=on.

Written with StackEdit.

References

How to Add Multiple Directories in GOPATH with VSCode?

Since go versions keep moving up, maintaining GOPATH inside a Go installation path is a pain.

export GOPATH=/home/a-user/go/1.14.9
ls /home/a-user/go/1.14.0/src 
bitbucket.org github.com    go.uber.org   golang.org    gopkg.in

Adding multiple directories in GOPATH

  1. Create a directory e.g. for work
    mkdir $HOME/mywork
    
  2. GOPATH assumes a structure in the directory, so create three directories inside.
    ls $HOME/mywork
    bin pkg src
    
  3. You will do all your personal development work inside $HOME/mywork/src.
    ls $HOME/mywork/src
    project-01
    
  4. Append .bashrc or .zshrc (append is a must, it won’t work otherwise because GOPATH is preset by goenv)
    # keep it at the end of the file
    export GOENV_ROOT="$HOME/.goenv"
    export PATH="$GOENV_ROOT/bin:$PATH"
    eval "$(goenv init -)"
    
    export PATH="$GOROOT/bin:$PATH"
    export PATH="$PATH:$GOPATH/bin"
    export PATH="$PATH:$HOME/bin"
    
    # add personal directory
    export GOPATH="$GOPATH:$HOME/mywork/"
    
  5. Open the directory $HOME/mywork/src/project-01 in VSCode workspace.
  6. If you have the VSCode extension for Go installed, restart VSCode.

If go to implementation doesn’t work, ensure that go is installed properly with src directory.

ls $HOME/go/1.14.0/ 
bin pkg src

That’s all folks!

References

VSCode Plugins for Golang Development

  1. Rich Go language support for Visual Studio Code
  2. Theme: Dark Knight: A simple dark theme for Golang projects
  3. Git Blame & Git History
  4. Postfix templates for Golang
  5. Adds go to implementation on context menu
  6. All-language autocompleter — TabNine uses machine learning to help you write code faster.

This is a minimal list of Go development extensions in my opinion.

vscode: How to Customize a Settings and Theme

You can change almost everything in vscode settings. I wasn’t happy with terminal settings, so found very useful hints on changing them.

A complete list of coding font is available here.

My current settings.json is as follows:

{
    "telemetry.enableTelemetry": false,
    "telemetry.enableCrashReporter": false,
    "editor.renderWhitespace": "all",
    "editor.wordWrap": "on",
    "editor.fontLigatures": true,
    "editor.fontFamily": "'Hack'",
    "editor.minimap.enabled": false,
    "breadcrumbs.enabled": false,
    "files.autoSave": "afterDelay",
    "workbench.statusBar.visible": true,
    "workbench.activityBar.visible": true,
    "git.autofetch": true,
    "python.jediEnabled": false,
    "workbench.colorTheme": "Slack Theme Aubergine - NEW COLOR",
    "window.zoomLevel": 0,
    "go.formatTool": "goimports",
    "go.useLanguageServer": true,
    "python.insidersChannel": "off",
    "C_Cpp.default.cppStandard": "c++11",
    "C_Cpp.default.cStandard": "c11",
    "C_Cpp.experimentalFeatures": "Enabled",
    "clang.diagnostic.enable": false,
    "C_Cpp.default.intelliSenseMode": "clang-x64",
    "terminal.integrated.rendererType": "dom",
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "explorer.confirmDelete": false,
    "workbench.preferredLightColorTheme": "Slack Theme Aubergine - NEW COLOR",
    "workbench.fontAliasing": "auto",
    "editor.fontSize": 11.75,
    "terminal.integrated.shell.osx": "/bin/zsh",
    "editor.fontWeight": "bold",
    "terminal.integrated.copyOnSelection": true,
    "terminal.integrated.cursorBlinking": true,
    "terminal.integrated.fontWeight": "bold",
    "terminal.explorerKind": "external",
    "terminal.integrated.drawBoldTextInBrightColors": false,
    "workbench.iconTheme": "vscode-great-icons",
    }

Refernces

Written with StackEdit.

Top 5 VSCode Extensions for Vim Lovers

Vim Extensions

  • vscodevim: Vim emulation for Visual Studio Code
  • Better Align (: + = in visual mode)

Language Specific Extensions

C/C++

  • C/C++ IntelliSense, debugging, and code browsing.
  • Better C++: better-cpp-syntax

Golang

  • Go Microsoft
  • Go Group Imports
  • Go Critic

Python

  • Python Microsoft
  • Python Extension Pack

Ruby

  • Ruby for Visual Studio Code
  • Rubocop
  • Ruby Solargraph

Themes

  • Github Plus
  • Winter Is Coming (Light & Dark)