Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func stringConst(file *ast.File) {
- var before, after []ast.Decl
- if len(file.Decls) > 0 {
- hasImport := false
- if genDecl, ok := file.Decls[0].(*ast.GenDecl); ok {
- hasImport = genDecl.Tok == token.IMPORT
- }
- if hasImport {
- before, after = []ast.Decl{file.Decls[0]}, file.Decls[1:]
- } else {
- after = file.Decls
- }
- }
- ast.Inspect(file, func(node ast.Node) bool {
- /*if BasicLit, ok := node.(*ast.BasicLit); ok {
- BasicLit.Kind == token.STRING {
- }
- }*/
- switch x := node.(type) {
- case *ast.BasicLit:
- if x.Kind == token.STRING {
- storage.values = string(x.Value)
- //storage.names = "const" + string()
- fmt.Sprintf("!!!!!!!!!!!!!!!!!!!!!!!!!!")
- }
- file.Decls = append(before,
- &ast.GenDecl{
- Tok: token.CONST,
- Specs: []ast.Spec{
- &ast.ValueSpec{
- //Doc: nil,
- Names: []*ast.Ident{ast.NewIdent("name")},
- Type: ast.NewIdent("string"),
- Values: []ast.Expr{
- &ast.BasicLit{
- Kind: token.STRING,
- Value: fmt.Sprintf("%s","\"" + storage.values + "\""),
- },
- },
- },
- },
- },
- )
- file.Decls = append(file.Decls, after...)
- }
- return true;
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement