codegen

package
v1.54.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

S is an alias to String. TODO: can we find a new name for this?

Functions

func Call

func Call(name string) *icall

Call is used to generate a function call.

func Const

func Const() *iconst

func For

func For(judge any) *ifor

func Function

func Function(name string) *ifunction

Function represent both method and function in Go.

NOTES

If `WithReceiver`, we will generate a method:

func (t test) Test()

If `WithCall`, we will generate a function call:

func Test(){}()

If `AddBody`, we will generate like a function definition without body:

func Test() {
    println("Hello, World!")
}

func If

func If(judge any) *iif

func Import

func Import() *iimport

Import will start a new import group.

func Interface

func Interface(name string) *iinterface

func LineComment

func LineComment(content string, args ...any) *istring

func Lit

func Lit(value any) *lit

func NewGroup

func NewGroup() *group
Example
f := NewGroup()
f.AddPackage("main")
f.NewImport().AddPath("fmt")
f.NewFunction("main").AddBody(
	String(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
Output:
package main

import "fmt"
func main(){
fmt.Println("Hello, World!")}

func Package

func Package(name string) *ipackage

func Return

func Return(node ...any) *ireturn

func String

func String(format string, args ...any) *istring

String will add a format string in NewGroup, just like fmt.Printf.

func Switch

func Switch(judge any) *iswitch

func Type

func Type(name string, typ any) *itype

func TypeAlias

func TypeAlias(name string, typ any) *itype

func Value

func Value(typ string) *ivalue

func Var

func Var() *ivar

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

func New

func New() *Generator

New will create a new generator which hold the group reference.

func (*Generator) AppendFile

func (g *Generator) AppendFile(path string) error

AppendFile will append the group after the give path.

func (*Generator) NewGroup

func (g *Generator) NewGroup() (ng *group)

func (*Generator) Write

func (g *Generator) Write(w io.Writer)

Write will write the group into the given writer.

func (*Generator) WriteFile

func (g *Generator) WriteFile(path string) error

WriteFile will write the group into the given path.

type IStruct

type IStruct struct {
	// contains filtered or unexported fields
}

func Struct

func Struct(name string) *IStruct

Struct will insert a new struct.

func (*IStruct) AddField

func (i *IStruct) AddField(name, typ any, tags map[string]string) *IStruct

func (*IStruct) AddLine

func (i *IStruct) AddLine() *IStruct

AddLine will insert an empty line.

func (*IStruct) AddLineComment

func (i *IStruct) AddLineComment(content string, args ...any) *IStruct

AddLineComment will insert a new line comment.

type Node

type Node interface {
	// contains filtered or unexported methods
}

func Continue

func Continue() Node

func Defer

func Defer(body any) Node

func Embed

func Embed(fn func() Node) Node

Embed accept a close clause to build a node.

func Line

func Line() Node

func Template

func Template(data any, tmpl string) Node

Jump to

Keyboard shortcuts

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