
Security News
US Government Forces Anthropic to Pull Claude Fable Days After Launch
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.
github.com/thorsphere/lpcode
Advanced tools
Go package for programmatic Go code generation that tries to keep it simple (KISS principle). The package is designed primarily to support the tserrgen code generator.
In the Go app, the package is imported with
import "github.com/thorsphere/lpcode"
Codefile manages the lifecycle of a code file. StartFile initializes the file with a header template, WriteCode writes generated code content, and FinishFile appends a footer template and formats the result. Header and footer files (with suffixes ".header" and ".footer") are required and are searched for in the codefile directory or the current directory. Header and footer files allow you to include package declarations, imports, licenses, or other boilerplate that should appear at the beginning or end of generated files.
func NewCodefile(dn tsfio.Directory, fn tsfio.Filename) (*Codefile, error)
func (cf *Codefile) StartFile() error
func (cf *Codefile) WriteCode(c *Code) error
func (cf *Codefile) FinishFile() error
There are helper functions for formatting, retrieving the code file filepath and for retrieving its contents as a string.
func (cf *Codefile) Format() error
func (cf *Codefile) Filepath() tsfio.Filename
func (cf *Codefile) String() string
A Code contains source code as string. A builder for constructing Go source code strings using method chaining. The Code type offers a range of methods for generating common Go syntax. The stored source code is amended by calling its methods. The source code can be retrieved with String and formatted with Format.
func NewCode() *Code
func (code *Code) LineComment(c string) *Code
func (code *Code) FuncEnd() *Code
func (code *Code) BlockEnd() *Code
func (code *Code) Call(n string) *Code
func (code *Code) ParamEndln() *Code
func (code *Code) ParamEnd() *Code
func (code *Code) Func1(a *Func1Args) *Code
func (code *Code) TypeStruct(n string) *Code
func (code *Code) VarSpec(a *VarSpecArgs) *Code
func (code *Code) List() *Code
func (code *Code) Listln() *Code
func (code *Code) SelField(a *SelArgs) *Code
func (code *Code) SelMethod(a *SelArgs) *Code
func (code *Code) If(a *IfArgs) *Code
func (code *Code) IfErr(a *IfErrArgs) *Code
func (code *Code) Return() *Code
func (code *Code) Addr() *Code
func (code *Code) Ident(n string) *Code
func (code *Code) Assignment(a *AssignmentArgs) *Code
func (code *Code) CompositeLit(LiteralType string) *Code
func (code *Code) ShortVarDecl(a *ShortVarDeclArgs) *Code
func (code *Code) KeyedElement(a *KeyedElementArgs) *Code
func (code *Code) Testvariables(t *Testvars) *Code
func (code *Code) Format() error
func (code *Code) String() string
package main
import (
"fmt"
"github.com/thorsphere/lpcode"
)
func main() {
c := lpcode.NewCode().
TypeStruct("foo").
VarSpec(&lpcode.VarSpecArgs{Ident: "A", Type: "int"}).
VarSpec(&lpcode.VarSpecArgs{Ident: "B", Type: "string"}).BlockEnd().
Func1(&lpcode.Func1Args{Name: "Example", Var: "x", Type: "int", Return: "error"}).
IfErr(&lpcode.IfErrArgs{Method: "doSomething()", Operator: "!="}).Return().Ident("err").BlockEnd().
If(&lpcode.IfArgs{ExprLeft: "x", Operator: ">", ExprRight: "10"}).
CompositeLit("foo").
KeyedElement(&lpcode.KeyedElementArgs{Key: "A", Elem: "1"}).
KeyedElement(&lpcode.KeyedElementArgs{Key: `B`, Elem: `"hello"`}).
BlockEnd().BlockEnd().Return().Ident("nil").FuncEnd()
c.Format()
fmt.Print(c)
}
Output:
type foo struct {
A int
B string
}
func Example(x int) error {
if err := doSomething(); err != nil {
return err
}
if x > 10 {
foo{A: 1,
B: "hello",
}
}
return nil
}
It does not perform syntax validation; the generated code should be validated by go/format or other tools.
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.

Security News
A network of 152 Chrome live wallpaper extensions hid ad tracking and made extension-driven traffic look like Google search clicks.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.