您在哪里存储可重复使用的模拟?

Could you please explain what is a correct way of organizing unit tests? For instance if would like to mock my struct dependencies I need to create a mock dependency which "implements" some interface.

Where should I create this mock? Should it be created in the same test file? But then what if I will need it in another test? Go doesn't allow to define struct with a same name (e.g. UserServiceMock) in 2 different files of the same package. Then what is the best place to define this mock struct?

And another question. Should I implement this kind of mocks by myself or there are some libraries / tools which allow me to do it?

I store my mocks in a mock package so I can call them from different test packages and use that package name in my tests as an indication that I am mocking a dependency. For example:

mock.UserService

You could create a generator or use GoMock

I also faced this issue and I resolved it by putting mocks of interface along with their declaration in a separate file.

mockery -dir=service  -all -inpkg

I used mockery to generate them and this will create mock in the same folder. As per the above command, it will generate a separate file for each interface exist in service directory. For e.g.: mock_MyInterface.go.

if you don't use -inpkg flag, it will create mocks in a default folder mocks but it does not add any prefix in mocked implementation and thus it will create problem when you will import mocks package in unit test because you will get conflict due to mock and original implementation with the same name.