0

I am new to golang How to create a struct and attributes dynamically from gocode, it has to store it as a file in the end.

For example:

Struct name: user By default, it has to create Name attribute

type User struct {
    Name string
}

it has to store as a file ex: user_struct.go

Could you please someone help to find a way to do it

3
  • Please clear what you actually trying to accomplish. Commented Apr 13, 2018 at 15:50
  • 2
    Use text/template to write Go code. Use go/printer to format it. Commented Apr 13, 2018 at 15:53
  • @Peter Could you please explain me with details or an example Commented Apr 13, 2018 at 15:58

2 Answers 2

2

Use text/template to write Go code. Since I don't know how you want to do this in detail, I'll use a trivial template in the example. Any kind of real world template is bound produce ill-formated code, but thanks to go fmt you pretty much only have to get the newlines right (leverage semicolons if you ever have trouble with that). go fmt uses go/printer under the hood, and you can too.

See the linked package documentation and examples for details.

package main

import (
    "bytes"
    "go/parser"
    "go/printer"
    "go/token"
    "html/template"
    "io"
    "log"
    "os"
)

var structTpl = `
    package main

    type {{ . }} struct {
            Name string
    }
    `

func main() {
    // Only do this once per template at the start of your program.
    // Then simply call Execute as necessary.
    tpl := template.Must(template.New("foo").Parse(structTpl))

    messy := &bytes.Buffer{}
    tpl.Execute(messy, "User")

    // Parse the code
    fset := &token.FileSet{}
    ast, err := parser.ParseFile(fset, "", messy, parser.ParseComments|parser.DeclarationErrors)
    if err != nil {
        log.Fatal(err)
    }

    // Print the code, neatly formatted.
    neat := &bytes.Buffer{}
    err = printer.Fprint(neat, fset, ast)
    if err != nil {
        log.Fatal(err)
    }

    io.Copy(os.Stdout, neat) // Or write to file as desired.
}

Try it on the playground: https://play.golang.org/p/YhPAeos4-ek

Sign up to request clarification or add additional context in comments.

Comments

0

If you are looking for ast based meta info about struct, receivers, etc you may consider the following:

package main
import  "github.com/viant/toolbox"

func main() {

    goCodeLocation := ".."
    fileSetInfo, err := toolbox.NewFileSetInfo(goCodeLocation)
    if err != nil {
        panic(err)
    }
    toolbox.Dump(fileSetInfo)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.