This repository provides examples of unique go generate
directives that can be inserted into any Go file to generate standardized mock filenames in the format filename_mock.go
. The directive doesn't need to be changed for each file. Additionally, an example of configuring the GoLand IDE is included, demonstrating nested files, which conveniently displays the project's content, and file watchers, allowing mock generation after each file save, providing a seamless experience.
-
In the
example_var.go
file, mockgen is utilized throughgo generate
variables (more details can be found ingo help generate
). This is a straightforward approach without using bash, but it creates files in the formatfilename.go_mock.go
. -
The
example_bash.go
file provides an example of mockgen using a command that separates the basename, allowing the creation of a file in the formatfilename_mock.go
. To use this method, bash must be correctly set up with GOPATH. An example for macOS is provided:nano $HOME/.bash_profile
(if the file does not exist - create it)- Insert into the file:
Specify your SDK; the example is for GoLand with Settings -> GOROOT -> go1.21.4 installed. You can try using the
export GOPATH=$HOME/go export GOROOT=$HOME/sdk/go1.21.4 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
go env
command if this does not work
- Save the file with
Control + X
->Y
- The command
cat $HOME/.bash_profile
should display the file's content. - The command
echo $PATH | tr ':' '\n'
should also display changes. - Verify that
go version
andmockgen -version
return the expected versions.
Example:
To configure a compact file display:
-
Click on the three dots to the right of the project menu (next to "select opened files" and "collapse files").
-
Choose "file nesting."
-
Find the
.go
pattern in the left column and add to the right column, alongside existing values:.go_mock.go; _easyjson.go; _mock.go; _reform.go; _string.go; _test.go
-
Save and observe the changes.
Link to File Watchers JetBrains Docs
File watchers allow actions to be performed upon saving a file, including auto-saving. To set up mock generation when a file changes:
- (Optional step) Ensure that the file watchers plugin is enabled in GoLand -> Settings -> Plugins -> Installed. Most likely, this plugin is already enabled.
- Go to GoLand -> Settings -> Tools -> File Watchers.
- Click the plus sign, then "."
- Fill in the fields:
- Program:
go
- Arguments:
generate
- Program:
- Optionally, set any additional checkboxes you need, save, and enable the file watcher.
Example of how a file watcher can be configured: