Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

github.com/thorsphere/lpcode

Package Overview
Dependencies
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/thorsphere/lpcode

Source
Go Modules
Version
v1.1.0
Version published
Created
Source

lpcode

Go Report Card CodeFactor OSS Lifecycle

PkgGoDev GitHub go.mod Go version Libraries.io dependency status for GitHub repo

GitHub release (latest by date) GitHub last commit GitHub commit activity GitHub code size in bytes GitHub Top Language GitHub

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.

  • Simple: A simple and fluent API with method chaining
  • Formatted: Automatic gofmt integration for clean output
  • Tested: Well-tested with unit tests with high code coverage
  • Dependencies: Minimal dependencies and only depends on Go Standard Library, tsfio and tserr

Usage

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) TestVarDecl(t *Testvars) *Code
func (code *Code) Format() error
func (code *Code) String() string

Example

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)
}

Run in Go Playground

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
}

Limitations

It does not perform syntax validation; the generated code should be validated by go/format or other tools.

Godoc

Go Report Card

Open Source Insights

FAQs

Package last updated on 05 Apr 2026

Did you know?

Socket

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.

Install

Related posts