Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.22 KB | None | 0 0
  1. func stringConst(file *ast.File) {
  2.     var before, after []ast.Decl
  3.  
  4.     if len(file.Decls) > 0 {
  5.         hasImport := false
  6.         if genDecl, ok := file.Decls[0].(*ast.GenDecl); ok {
  7.             hasImport = genDecl.Tok == token.IMPORT
  8.         }
  9.  
  10.         if hasImport {
  11.             before, after = []ast.Decl{file.Decls[0]}, file.Decls[1:]
  12.         } else {
  13.             after = file.Decls
  14.         }
  15.     }
  16.  
  17.     ast.Inspect(file, func(node ast.Node) bool {
  18.         /*if BasicLit, ok := node.(*ast.BasicLit); ok {
  19.             BasicLit.Kind == token.STRING {
  20.  
  21.             }
  22.         }*/
  23.  
  24.         switch x := node.(type) {
  25.  
  26.         case *ast.BasicLit:
  27.             if x.Kind == token.STRING {
  28.                 storage.values = string(x.Value)
  29.                 //storage.names = "const" + string()
  30.                 fmt.Sprintf("!!!!!!!!!!!!!!!!!!!!!!!!!!")
  31.  
  32.             }
  33.             file.Decls = append(before,
  34.                 &ast.GenDecl{
  35.                     Tok: token.CONST,
  36.                     Specs: []ast.Spec{
  37.                         &ast.ValueSpec{
  38.                             //Doc:   nil,
  39.                             Names: []*ast.Ident{ast.NewIdent("name")},
  40.                             Type:  ast.NewIdent("string"),
  41.                             Values: []ast.Expr{
  42.                                 &ast.BasicLit{
  43.                                     Kind:  token.STRING,
  44.                                     Value: fmt.Sprintf("%s","\"" + storage.values + "\""),
  45.                                 },
  46.                             },
  47.                         },
  48.                     },
  49.                 },
  50.  
  51.             )
  52.             file.Decls = append(file.Decls, after...)
  53.         }
  54.  
  55.         return true;
  56.  
  57.     })
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement