package service import ( "go/ast" "go/token" ) var InConstBlock = false func handleGenDecl(decl ast.Decl) string { gd := decl.(*ast.GenDecl) code := "" switch gd.Tok { case token.CONST: InConstBlock = true code += handleSpecs(gd.Specs) InConstBlock = false default: code += handleSpecs(gd.Specs) } return code } func handleSpecs(specs []ast.Spec) (code string) { for _, spec := range specs { if InConstBlock { code += "const " } switch s := spec.(type) { case *ast.ImportSpec: code += handleImportSpec(spec) case *ast.ValueSpec: code += handleValueSpec(s) + ";" case *ast.TypeSpec: code += handleTypeSpec(spec) } } return } func handleImportSpec(spec ast.Spec) string { s := spec.(*ast.ImportSpec) code := "" if s.Name != nil { name := handleIdentExpr(s.Name) if val, ok := mapping[name]; ok { name = val } if name != "" { if name != "controller" { code = "#include <" + name + ".h>\n" } } } return code }