astgen

package module
v0.0.0-...-e1c22b9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 8 Imported by: 6

README

astgen-go

CI Status Go Report Card MIT License pkg.go.dev

Build Go code from arbitrary value in Go.

Usage

package main

import (
	"go/printer"
	"go/token"
	"log"
	"os"

	"github.com/itchyny/astgen-go"
)

type X struct {
	x int
	y Y
	z *Z
}

type Y struct {
	y int
}

type Z struct {
	s string
	t map[string]int
}

func main() {
	x := &X{1, Y{2}, &Z{"hello", map[string]int{"x": 42}}}
	t, err := astgen.Build(x)
	if err != nil {
		log.Fatal(err)
	}
	err = printer.Fprint(os.Stdout, token.NewFileSet(), t)
	if err != nil {
		log.Fatal(err)
	}
}
&X{x: 1, y: Y{y: 2}, z: &Z{s: "hello", t: map[string]int{"x": 42}}}

Bug Tracker

Report bug at Issues・itchyny/astgen-go - GitHub.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(x any) (ast.Node, error)

Build ast from any.

Example
package main

import (
	"go/printer"
	"go/token"
	"log"
	"os"

	"github.com/itchyny/astgen-go"
)

type X struct {
	x int
	y Y
	z *Z
}

type Y struct {
	y int
}

type Z struct {
	s string
	t map[string]int
}

func main() {
	x := &X{1, Y{2}, &Z{"hello", map[string]int{"x": 42}}}
	t, err := astgen.Build(x)
	if err != nil {
		log.Fatal(err)
	}
	err = printer.Fprint(os.Stdout, token.NewFileSet(), t)
	if err != nil {
		log.Fatal(err)
	}

}
Output:

&X{x: 1, y: Y{y: 2}, z: &Z{s: "hello", t: map[string]int{"x": 42}}}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL