How can I get syntax highlighting for cgo (golang)

Viewed 1291

cgo is written in "comments" in go which means by default it's given comment syntax highlighting. It would be nice have proper golang and C syntax highlighting but within cgo files.

package main

// ... C code or #include here ...
import "C"

... Go code here ...

Example

I'd like this for either Visual Studio Code, or ViM.

How can this be achieved?

2 Answers

One way is to place the C code in a header file, for instance example.h, then in your Go program use:

// #include "example.h"
import "C"

When opening example.h, you get syntax highlighting.

Alternatively, for ViM, the SyntaxRange plugin can supposedly highlight C code that is part of Go code, but it may not be straightforward to configure.

Related