zsh: command not found: mockgen - golang 1.18 - macOS Monterrey
Asked Answered
M

6

9

I'm new to go. Currently I'm using zsh terminal in macOS, just followed the instructions pointed out here https://github.com/golang/mock when installing go mock. However when trying to execute a mockgen command I keep seeing zsh: command not found: mockgen and when navigating in the terminal to my $GOPATH/bin i see mockgen inthere, so I don't know if there's anything else needed.

enter image description here

These are the variables I have configured in my /.zshrc file:

#GO path
export GOPATH="$HOME/Documents/study_projects/go"
export GOBINPATH="$GOPATH/bin"

Idk if GOBINPATH is a proper name for this $GOPATH/bin variable to be exported to the PATH also, aso pointed out in https://github.com/golang/mock (mockgen/gomock) installation instructions. Does anyone of you know what else is needed here, do I need an additional configuration for this mockgen command to work with zsh?

Thank you!

Mirellamirelle answered 30/5, 2022 at 5:21 Comment(1)
Have you set PATH? Like: export PATH=$PATH:$GOROOT/bin:$GOPATH/binDyspepsia
F
17

add export PATH=$PATH:$(go env GOPATH)/bin in .zshrc and try again.

Footpad answered 2/11, 2022 at 16:12 Comment(2)
And remember to source .zshrc, maybe you also need to reload your terminal.Yim
you can add alias load="source ~/.zshrc" in your ~/.zshrc file, then each time you make change in the file you can update terminal with load command without restarting a terminalPrine
U
13
  1. Make sure run below for go 1.16+

    go install go.uber.org/mock/mockgen@latest

  2. Add users/[your_login]/go/bin to .zshrc path

Utilitarianism answered 28/7, 2022 at 7:2 Comment(1)
Its now go install go.uber.org/mock/mockgen@latestDispenser
C
2

What version of Golang are you using? For Go version 1.16+, you don't need to use GOPATH In my case, the binary was installed in this path: /Users/USERNAME/go/bin/mockgen so I added it to my $PATH in .zshrc.

HTH!

Coyotillo answered 15/6, 2023 at 4:54 Comment(0)
D
2

I did the following things to sort it for my MacBook M3 chip

Install mockgen

go install github.com/golang/mock/mockgen@latest

This will place the mockgen executable in your GOBIN directory. By default, GOBIN is $GOPATH/bin if GOBIN is not explicitly set.

Add GOBIN to Your PATH

If GOBIN is not already in your PATH, you need to add it. First, determine the correct directory:

echo $GOBIN

If GOBIN is not set, the default is $GOPATH/bin:

echo $GOPATH/bin

Add the Path to Your ~/.zshrc

nano ~/.zshrc
export PATH=$PATH:$(go env GOPATH)/bin

Alternatively, if GOBIN is set:

export PATH=$PATH:$GOBIN

Apply the Changes

source ~/.zshrc

Verify mockgen Installation

mockgen -version

Executing mockgen

mockgen -source=<path to source file> -destination=<path to destination file> -package=<mock package name>
Directly answered 10/6 at 6:38 Comment(1)
This worked. Although I had the export in bashrc, since I used zshrc, I had to add it there too.Mclin
C
0

My mockgen got installed under vim /Users/xxx/go/bin/darwin_amd64/ instead of /Users/xxx/go/bin so I moved it to bin folder and it worked!

cp /Users/xxx/go/bin/darwin_amd64/mockgen /Users/xxx/go/bin/ 
Cordon answered 31/5, 2023 at 21:43 Comment(1)
Maybe you should check the $GOPATH ser in your system.Mclin
K
-1

Had the same problem. Tried to add mockgen to the $PATH, didn't help.

Just added the path directly in command line:

/Users/{username}/go/bin/mockgen -source={source_path}.go -destination={destination_path}.go -package=mock {interfaces}

Worked for me

Kissable answered 4/2 at 19:29 Comment(1)
Thats the failsafe way for remote testing!Mclin

© 2022 - 2024 — McMap. All rights reserved.