40 строки
652 Б
Go
40 строки
652 Б
Go
package service
|
|
|
|
import "go/ast"
|
|
|
|
var (
|
|
constDeclarations []*Const
|
|
)
|
|
|
|
type Const struct {
|
|
s *ast.ValueSpec
|
|
}
|
|
|
|
func NewConst(s *ast.ValueSpec) *Const {
|
|
return &Const{s: s}
|
|
}
|
|
|
|
func (c *Const) String() (code string) {
|
|
// return c.s.Names[0].Name
|
|
return c.generate()
|
|
}
|
|
|
|
func (c *Const) generate() (code string) {
|
|
code += "const "
|
|
code += handleLocalVariable(c.s)
|
|
return
|
|
}
|
|
|
|
func handleConstSpecs(specs []ast.Spec) string {
|
|
for _, spec := range specs {
|
|
switch s := spec.(type) {
|
|
case *ast.ValueSpec:
|
|
addConstant(s)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
func addConstant(s *ast.ValueSpec) {
|
|
c := NewConst(s)
|
|
constDeclarations = append(constDeclarations, c)
|
|
}
|