xstd

command module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2020 License: MIT Imports: 12 Imported by: 0

README

xstd

A Go code generator for extending standard packages

Usage

Make a directory which has name same with standard package you want to extend and generate a file using xstd:

$ mkdir context
$ cd context
$ xstd -pkg context > std_gen.go

Then start to extend the package and use it. You can just replace your import spec of the standard package to the package generated above.

excludes option

If you want to redefine your extended version of constant/variable/function/type defined in standard package, you can use -excludes option. The option takes a list of constant/variable/function/type names separated by comma, and excludes these from generated file so you can define your own version but which has same name with standard package's one.

Example

Here is an example with context package.

$ mkdir context
$ cd context
$ xstd -pkg context > std_gen.go

Then create a source code file with content below:

package context

type requestIDKey struct{}

// WithRequestID returns a copy of given context in which associated with the request ID.
func WithRequestID(parent Context, requestID string) Context {
	return WithValue(parent, requestIDKey{}, requestID)
}

// RequestID returns the request ID associated with the context, or empty string if no
// request ID is associated with the context using WithRequestID.
func RequestID(ctx context.Context) string {
	if requestID, ok := ctx.Value(requestIDKey{}).(string); ok {
		return requestID
	}

	return ""
}

Now you can use this local context package with same way of standard context package, but local one has two extra functions named WithRequestID and RequestID.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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