Mayank Patel
Mayank Patel

Reputation: 8536

Go Module : main.go file is being overwritten after go build

I am facing an weird issue where after vendorising my dependencies with go modules, go build command is overriding the main.go file with random data.

Start of the file looks like this:

����
H
H__PAGEZEROx__TEXTpxpx__text__TEXT��7�__rodata__TEXT��7Y��7__symbol_stub1__TEXT�V��V�__typelink__TEXT�V�(�V__itablink__TEXTP@V�
P@V__gosymtab__TEXT�JV�JV__gopclntab__TEXTKV
"KV�__DATApx�|px c
__nl_symbol_ptr__DATApx�pxs__noptrdata__DATA�sx���sx__data__DATA�&{���&{__bss__DATA �{��__noptrbss__DATA�}�2__DWARF�}�{��   __zdebug_abbrev__DWARF�}�{__zdebug_line__DWARF�}'W�{__zdebug_frame__DWARF;H���;8�__zdebug_pubname__DWARF�څ�K�ʃ__zdebug_pubtype__DWARF�&����__debug_gdb_scri__DWARF��6�ӄ__zdebug_info__DWARF���
�ӄ__zdebug_loc__DWARF䉑��y�__zdebug_ranges__DWARFU��ZE�H__LINKEDIT�}L���L��*�����,�r���Pu,u,x,xo�� /usr/lib/dyld8/usr/lib/libSystem.B.dylibh/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation`/System/Library/Frameworks/Security.framework/Versions/A/Security$

� Go build ID: "OLftMbjtv5aWMkI_0qrD/LhWRtD0wcaKFWRYSDOa9/7dFWcNOQ4BpWWqZQW07D/pkR9ABiz-SHIBaJIZ1ur"
 ����������UH��AWAVATSH���=|I��

Go version: go version go1.12.5 darwin/amd64

Enabled Go moduled with export GO111MODULE=on

Folder structure: go.mod go.sum log.go main.go vendor

FYI: only main.go is being overwritten, not log.go.

Go module initialised with go mod init

App vendorised with go mod vendor

Not sure if I am doing something wrong. Any help is appreciated.

Upvotes: 1

Views: 829

Answers (1)

kasvith
kasvith

Reputation: 377

The module name should not be main.go as it is a file inside a package. Please use your project name for module name in go mod init.

For example, if your project root is hello, name your package hello, not main.go. Also if you want to use the module over and over again, please consider using your repository name as the module name.

Go handles packages not individual files. Go modules are used to organize packages. Know more in official documentation

Upvotes: 3

Related Questions