Skip to content

Instantly share code, notes, and snippets.

@holmeszyx
Created May 13, 2016 08:51
Show Gist options
  • Save holmeszyx/aca7cf3fd56492610411d68a52ab245e to your computer and use it in GitHub Desktop.
Save holmeszyx/aca7cf3fd56492610411d68a52ab245e to your computer and use it in GitHub Desktop.
生成一下java的mask或者index常量
package main
import (
"fmt"
"strings"
)
type item struct {
Name string
Comment string
}
func createMasks(src []item) {
//var index uint
for index, item := range src {
if index > 0 {
fmt.Println()
}
if item.Comment != "" {
fmt.Printf("/** %s */\n", item.Comment)
}
fmt.Printf("public static final int MASK_%s = %d;\n", strings.ToUpper(item.Name), 1<<uint(index))
index++
}
}
func createIndex(src []item, fromIndex int) {
//var index uint
for index, item := range src {
if index > 0 {
fmt.Println()
}
if item.Comment != "" {
fmt.Printf("/** %s */\n", item.Comment)
}
fmt.Printf("public static final int INDEX_%s = %d;\n", strings.ToUpper(item.Name), index + fromIndex)
index++
}
}
func main() {
m := make([]item, 0, 32)
add := func (n, c string) {
m = append(m, item{
Name : n,
Comment : c,
})
}
add("type_int", "int")
add("type_long", "long")
add("type_boolean", "boolean")
add("type_float", "float")
add("type_string", "string")
add("type_string_set", "string_set")
createMasks(m)
fmt.Print("\n\n// ============ index ===========\n\n")
createIndex(m, 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment