Created
February 27, 2018 06:39
-
-
Save dgryski/a8353cb0977641f1feb6db1b8ed0eaf7 to your computer and use it in GitHub Desktop.
using the C pre-processor with Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
laptop:cppgo dgryski$ cat gen.go | |
package main | |
//go:generate cpp-7 -E -P foo.gopp foo.go | |
laptop:cppgo dgryski$ cat foo.gopp | |
package main | |
import "fmt" | |
#define STRINGIFY(x) #x | |
#define TOSTRING(x) STRINGIFY(x) | |
#define AT __FILE__+":"+TOSTRING(__LINE__) | |
#define OUT(...) fmt.Println(AT, __VA_ARGS__) | |
#define ERR(e) if e != nil { return e } | |
func greeting() (string, error) { | |
return "hello", nil | |
} | |
func print() error { | |
s, err := greeting() | |
ERR(err) | |
OUT(s) | |
return nil | |
} | |
func main() { | |
print() | |
} | |
laptop:cppgo dgryski$ go generate | |
laptop:cppgo dgryski$ cat foo.go | |
package main | |
import "fmt" | |
func greeting() (string, error) { | |
return "hello", nil | |
} | |
func print() error { | |
s, err := greeting() | |
if err != nil { return err } | |
fmt.Println("foo.gopp"+":"+"21", s) | |
return nil | |
} | |
func main() { | |
print() | |
} | |
laptop:cppgo dgryski$ go run foo.go | |
foo.gopp:21 hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had 5 mins of free time and I wanted to give it a try (with no Go experience whatsoever) so I made a small cast using
asciinema
👍