Skip to content
A very simple Go source file pre-processor
Go Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
.gitignore
Makefile
README.md
go.mod
main.go

README.md

preprep-go

A very simple Go source file pre-processor to ...

It works, but the docs are still work in progress.

Why?

Why not? (I'll explain later ...)

How?

preprep-go will convert comments in go files to cpp compatible directive blocks.

Installation

go get -u github.com/iwittkau/preprep-go

This will install preprep-go in $HOME/go/bin/.

Example

Clone this repository and run go generate ./... from the project root.

Generate a .pgo file

The go:generate directive will convert the main.go to a main.pgo using preprep-go.

//go:generate preprep-go -i main.go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello from main")
	//#ifdef FEATURE
	feature()
	//#endif
}

This will generate a main.pgo like this:

//go:generate preprep-go -i main.go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello from main")
#ifdef FEATURE
	feature()
#endif
}

Generate a new .go file from a .pgo file, using cpp

You can now use cpp to convert this main.pgo to a new main.go. The Makefile in this repository uses a different folder for this:

mkdir build
cpp -DFEATURE -P example/main.pgo build/main.go

Now, there is the preprocessed main.go with the feature() call, because we have set FEATURE

//go:generate preprep-go -i main.go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello from main")

	feature()

}

Compiling the final binary

Our new, pre-processed main.go calls feature() which is also part of the main package, but located in the feature.go file. This file needs to be present when we want to build example with feature. Also, there needs to be a build tag set in feature.go.

cp example/feature.go build/
go build -tags feature -o xmpl-feat ./build
rm -r build

Best Practice

  • use a "build" folder, add all files and build the whole folder

Ceavats

  • all features need a build tag
  • without preprocessing only a binary with all features can be built
    • example/ will not compile unless all tags are provided during build

Inspiration

This article by Svetlin Ralchev inspired preprep-go:

Go does not have a preprocessor to control the inclusion of platform specific code. Even though C preprocessor is intended to be used only with C, C++, and Objective-C source code, we will use it as a general text processor of Go source code.

Svetlin Ralchev - Conditional compilation in Golang

You can’t perform that action at this time.