如何“动态”评估外部go源代码?

I need to parse a Go source code file, find a specific type (by name) and use it in my program. I already managed to find the type I need using the go/ast package but I don't know how to "load" it into my program so that I can use it.

Question: What's the best way to extract and use a type from an external source code file and use it on runtime?

I can't think of anything except an ugly method to basically copy the file, modify it by injecting a "main" function with my encoding stuff which sends the result to stdOut, execute the it, collect the encoded data from stdout, delete the modified file.

Use case: Analyse go source code and encode the types in a specific format (e.g. json schema)

Edit: Here is some code. The question is how to encode type allTypes (zero value) and then print it.

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
    "encoding/json"
)
    var src string = `
package mypack

type allTypes struct{
    Brands Brands
    Colours Colours
}
type Brands struct{
    Sony string
    Apple string
}

type Colours struct{
    Red string
    Green string
}

`
type sometype struct{
    Nothing int
}
func main() {
    // src is the input for which we want to inspect the AST.

    // Create the AST by parsing src.
    fset := token.NewFileSet() // positions are relative to fset
    f, err := parser.ParseFile(fset, "src.go", src, 0)
    if err != nil {
        panic(err)
    }

    // Inspect the AST and find our function
    var tp ast.TypeSpec
    ast.Inspect(f, func(n ast.Node) bool {
        switch x := n.(type) {
        case *ast.TypeSpec:
            if x.Name.Name == "allTypes"{
                tp = *x
            }
        }
        return true
    })

    fmt.Printf("We found the type: it is %v", tp)
    // Encode the zero value of sometype
    x := sometype{}
    b, _ := json.Marshal(&x)
    fmt.Printf("
 Zero value of someType (json) %s", b)
    //Next/Question: How to encode the zero value of "allTypes" ???
}

Also on playground

If you need to extract type information at runtime, you need to use the reflect package. This is the way Go's encoding/json and other similar packages work.

If you want to operate on the types defined in a Go source file, you can use the go.parser package to read and parse a source file into an AST, then traverse the AST for the elements you want to examine.

It seems there is no way to load the type information extracted from the source code(please correct me if I'm wrong). The only solution is to create/generate a package (main), inject it with all the types extracted from the target source code file, build/compile it, execute it and collect the encoded data from stdout.

If I understand you are asking for dynamic type loading ala Java's Class.forName(String className). The short answer is Go doesn't support this.

The correct way, as Nick Johnson pointed out, is to parse the tree using ast, and then "generate" the JSON yourself. You will not be able to "load" the type and use JSON.Marshal. It is also worth noting that any type which supports the json.Marshaler interface may generate custom JSON. You also need to ignore, but mark optional "omitempty" behavior. This really prevents you from using the compile it and hack through "stdout" behavior as well.