Skip to content

Instantly share code, notes, and snippets.

@dgryski
Created February 27, 2018 06:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgryski/a8353cb0977641f1feb6db1b8ed0eaf7 to your computer and use it in GitHub Desktop.
Save dgryski/a8353cb0977641f1feb6db1b8ed0eaf7 to your computer and use it in GitHub Desktop.
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
Copy link

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