Skip to content

Instantly share code, notes, and snippets.

@Karitham
Created May 16, 2021 17:11
Show Gist options
  • Save Karitham/79fa62b4effac893fe0ec7b4840b1d04 to your computer and use it in GitHub Desktop.
Save Karitham/79fa62b4effac893fe0ec7b4840b1d04 to your computer and use it in GitHub Desktop.
Export all struct fields of a go file
package main
import (
"bytes"
"fmt"
"os"
"regexp"
"strings"
"github.com/iancoleman/strcase"
)
var in = regexp.MustCompile("\t(\\w+)")
func main() {
input, err := os.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
locs := in.FindAllIndex(input, 1<<31)
for _, i := range locs {
buf := bytes.Buffer{}
buf.Grow(i[1] - i[0])
buf.WriteRune('\t')
buf.WriteString(strcase.ToCamel(string(input[i[0]:i[1]])))
buf.WriteString(strings.Repeat(" ", buf.Cap()-buf.Len()))
for j := 0; j < i[1]-i[0]; j++ {
input[i[0]+j] = buf.Bytes()[j]
}
}
fmt.Print(string(input))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment