Instantly share code, notes, and snippets.

Embed
What would you like to do?
using the C pre-processor with Go
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
@McNight

This comment has been minimized.

Show comment
Hide comment
@McNight

McNight Feb 28, 2018

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 👍

McNight commented Feb 28, 2018

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment