Golang : Build and compile multiple source files
One of the many challenges for new comers to Golang is to learn how to use go install
command to compile multiple source files. Most learned on how to compile and build individual files initially and usually hit stumbling block when they want to expand their projects to include additional source code files. Chances are that the source code files are not organized in the proper way.
From the official documentation, it is recommended that new Golang project to follow the open source code style workspace. A workspace typically has :
src
contains Go source files organized into packages (one package per directory),pkg
contains package objects, andbin
contains executable commands.
(see more at https://golang.org/doc/code.html#Workspaces )
and source codes files should be placed in the src
directory. For example :
MyGoProjects/src/program-name/main.go
MyGoProjects/src/program-name/functions.go
and remember to add the MyGoProject/src
to GOPATH.
Once this is done, you can execute the command :
>go install program-name
to build your Golang program. All the source files will now be compiled in object files, linked and finally the binary files will appear in the bin
directory.
Reference :
See also : Golang : Call function from another package
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+7.7k Golang : Variadic function arguments sanity check example
+8.4k Golang : Random integer with rand.Seed() within a given range
+13.3k Golang : How to determine if a year is leap year?
+26.1k Golang : Encrypt and decrypt data with AES crypto
+18.5k Golang : Read input from console line
+4.4k Linux/MacOSX : How to symlink a file?
+29.2k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+10.4k Golang : Get currencies exchange rates example
+8.6k Golang : Get curl -I or head data from URL example
+16k Golang :Trim white spaces from a string
+6.8k Golang : Get environment variable