Go object files

Go object files

C developers will be familiar with compiling C source files (.c) into object files (.o) before linking them into their final form. Did you know you can do the same thing with the Go compiler as well? This project is a quick demonstration of how to turn main.go into main.o!

The files

This project contains the following Go sources:

  • main.go: a simple Go program for summing two integers
  • sum.go: the function SumInt used by main.go

The program

Of course it is possible to build this project with go build:

go build -o sum

and then use the resulting binary executable to sum two integers:

$ ./sum 2 3
5

Object files

However, what does go build actually do? Well, one way to figure this out is by using go build -x to print the commands that go build executes. One such step is to produce object files for the Go sources:

go tool compile sum.go

The above command will produce the object file sum.o:

$ ls *.o
sum.o

But what happens when we try to create the object file for main.go?

$ go tool compile main.go
main.go:37:14: undefined: SumInt

In fact, this is the same error we would receive if we tried to use go run to run the program with just main.go:

$ go run main.go 2 3
# command-line-arguments
./main.go:37:14: undefined: SumInt

In order to create the object file or compile the program, Go has to create a dependency graph for all the files that depend on one another. The symbol used by main.go, SumInt, is in the file sum.go. If we were to compile main.o using both main.go and sum.go, then it would work.

First let's clean up the previous object file we created:

rm -f sum.o

Now let's compile main.go into an object file:

go tool compile main.go sum.go

Verify the result:

$ ls *.o
main.o

And main.o is the only object file created.

Conclusion

This project does not dig into a fraction of what is possible with go tool compile or look at go tool link at all. However, most developers will never need to manually execute these commands. That's the beauty of go build. Still, beauty is not only skin-deep, it's what is on the inside that counts, and understanding how go build works is a great goal to have!

Owner
Andrew Kutz
A father, husband, friend, son, and VMware engineer.
Andrew Kutz
Similar Resources

A Go io/fs filesystem implementation for reading files in a Github gists.

GistFS GistFS is an io/fs implementation that enables to read files stored in a given Gist. Requirements This module depends on io/fs which is only av

Oct 14, 2022

Create ePub files from URLs

url2epub Create ePub files from URLs Overview The root directory provides a Go library that creates ePub files out of URLs, with limitations.

Nov 5, 2022

🏵 Gee is tool of stdin to each files and stdout

🏵 Gee is tool of stdin to each files and stdout

Gee is tool of stdin to each files and stdout. It is similar to the tee command, but there are more functions for convenience. In addition, it was written as go. which provides output to stdout and files.

Nov 17, 2022

Golang PDF library for creating and processing PDF files (pure go)

UniPDF - PDF for Go UniDoc UniPDF is a PDF library for Go (golang) with capabilities for creating and reading, processing PDF files. The library is wr

Dec 28, 2022

Format /etc/fstab files.

Format /etc/fstab files.

Format /etc/fstab files. Features and limitations Can format /etc/fstab files. Will use 2 spaces between all fields, if they are of equal length. The

Dec 3, 2022

Run a command when files change

Reflex Reflex is a small tool to watch a directory and rerun a command when certain files change. It's great for automatically running compile/lint/te

Dec 29, 2022

Go (golang) library for reading and writing XLSX files.

XLSX Introduction xlsx is a library to simplify reading and writing the XML format used by recent version of Microsoft Excel in Go programs. Current s

Jan 5, 2023

Golang library for reading and writing Microsoft Excel™ (XLSX) files.

Golang library for reading and writing Microsoft Excel™ (XLSX) files.

Excelize Introduction Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX / XLSM / XLT

Dec 31, 2022

Easily create & extract archives, and compress & decompress files of various formats

archiver Introducing Archiver 3.1 - a cross-platform, multi-format archive utility and Go library. A powerful and flexible library meets an elegant CL

Jan 3, 2023
Split text files into gzip files with x lines

hakgzsplit split lines of text into multiple gzip files

Jun 21, 2022
Easily create Go files from stub files

go-stubs Easily create .go files from stub files in your projects. Usage go get github.com/nwby/go-stubs Create a stub file: package stubs type {{.Mo

Jan 27, 2022
app-services-go-linter plugin analyze source tree of Go files and validates the availability of i18n strings in *.toml files

app-services-go-linter app-services-go-linter plugin analyze source tree of Go files and validates the availability of i18n strings in *.toml files. A

Nov 29, 2021
Compute message digest for large files in Go

checksum test coverage Compute message digest, like MD5 and SHA256, in golang for potentially large files. Usage package main import ( "fmt" "githu

Dec 28, 2022
copy files for humans

Go-Decent-Copy go-decent-copy provides a copy file for humans Usage package main import "github.com/hugocarreira/go-decent-copy" func main() { e

Sep 26, 2022
Golang wrapper for Exiftool : extract as much metadata as possible (EXIF, ...) from files (pictures, pdf, office documents, ...)

go-exiftool go-exiftool is a golang library that wraps ExifTool. ExifTool's purpose is to extract as much metadata as possible (EXIF, IPTC, XMP, GPS,

Dec 28, 2022
Load GTFS files in golang

go-gtfs Load GTFS files in Go. The project is in maintenance mode. It is kept compatible with changes in the Go ecosystem but no new features will be

Dec 5, 2022
An implementation of the FileSystem interface for tar files.

TarFS A wrapper around tar.Reader. Implements the FileSystem interface for tar files. Adds an Open method, that enables reading of file according to i

Sep 26, 2022
QueryCSV enables you to load CSV files and manipulate them using SQL queries then after you finish you can export the new values to a CSV file
QueryCSV enables you to load CSV files and manipulate them using SQL queries then after you finish you can export the new values to a CSV file

QueryCSV enable you to load CSV files and manipulate them using SQL queries then after you finish you can export the new values to CSV file

Dec 22, 2021