diff --git a/Makefile b/Makefile index 9d21b7b..a08d455 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,10 @@ readme: godoc2md github.com/davecheney/godoc2md > README.md examples: - godoc2md github.com/kr/fs > examples/fs/README.md - godoc2md github.com/codegangsta/martini > examples/martini/README.md - godoc2md github.com/gorilla/sessions > examples/sessions/README.md - godoc2md go/build > examples/build/README.md + godoc2md -ex github.com/kr/fs > examples/fs/README.md + godoc2md -ex github.com/codegangsta/martini > examples/martini/README.md + godoc2md -ex github.com/gorilla/sessions > examples/sessions/README.md + godoc2md -ex github.com/pkg/errors > examples/errors/README.md + godoc2md -ex go/build > examples/build/README.md .PHONY: examples readme all diff --git a/example.go b/example.go new file mode 100644 index 0000000..52999c1 --- /dev/null +++ b/example.go @@ -0,0 +1,110 @@ +package main + +import ( + "bytes" + "go/doc" + "go/printer" + "regexp" + "sort" + "strings" + "unicode" + "unicode/utf8" + + "golang.org/x/tools/godoc" +) + +type example struct { + Name string + Doc string + Code string + Output string +} + +func examplesFunc(info *godoc.PageInfo, name string) []*example { + if !*showExamples { + return nil + } + var egs []*example + for _, eg := range info.Examples { + if name != "*" && stripExampleSuffix(eg.Name) != name { + continue + } + doc := eg.Doc + out := eg.Output + code, wholeFile := exampleCode(info, eg) + if wholeFile { + doc = "" + out = "" + } + egs = append(egs, &example{ + Name: eg.Name, + Doc: doc, + Code: code, + Output: out, + }) + } + sort.Slice(egs, func(i int, j int) bool { + ni, si := splitExampleName(egs[i].Name) + nj, sj := splitExampleName(egs[j].Name) + if ni == nj { + return si < sj + } + return ni < nj + }) + return egs +} + +var exampleOutputRx = regexp.MustCompile(`(?i)//[[:space:]]*(unordered )?output:`) + +func exampleCode(info *godoc.PageInfo, eg *doc.Example) (code string, wholeFile bool) { + // Print code + var buf bytes.Buffer + cnode := &printer.CommentedNode{Node: eg.Code, Comments: eg.Comments} + config := &printer.Config{Mode: printer.UseSpaces, Tabwidth: *tabWidth} + config.Fprint(&buf, info.FSet, cnode) + code = strings.Trim(buf.String(), "\n") + wholeFile = true + + if n := len(code); n >= 2 && code[0] == '{' && code[n-1] == '}' { + wholeFile = false + // Remove surrounding braces. + code = strings.Trim(code[1:n-1], "\n") + // Remove output from code. + if loc := exampleOutputRx.FindStringIndex(code); loc != nil { + code = strings.TrimRightFunc(code[:loc[0]], unicode.IsSpace) + } + // Unindent code. + lines := strings.Split(code, "\n") + unindent(lines) + code = strings.Join(lines, "\n") + } + + return code, wholeFile +} + +func splitExampleName(s string) (name, suffix string) { + i := strings.LastIndex(s, "_") + if 0 <= i && i < len(s)-1 && !startsWithUppercase(s[i+1:]) { + name = s[:i] + suffix = " (" + strings.Title(s[i+1:]) + ")" + return + } + name = s + return +} + +// stripExampleSuffix strips lowercase braz in Foo_braz or Foo_Bar_braz from name +// while keeping uppercase Braz in Foo_Braz. +func stripExampleSuffix(name string) string { + if i := strings.LastIndex(name, "_"); i != -1 { + if i < len(name)-1 && !startsWithUppercase(name[i+1:]) { + name = name[:i] + } + } + return name +} + +func startsWithUppercase(s string) bool { + r, _ := utf8.DecodeRuneInString(s) + return unicode.IsUpper(r) +} diff --git a/examples/build/README.md b/examples/build/README.md index f888131..08f89f7 100644 --- a/examples/build/README.md +++ b/examples/build/README.md @@ -117,6 +117,8 @@ During a particular build, the following words are satisfied: - "go1.6", from Go version 1.6 onward - "go1.7", from Go version 1.7 onward - "go1.8", from Go version 1.8 onward + - "go1.9", from Go version 1.9 onward + - "go1.10", from Go version 1.10 onward - any additional words listed in ctxt.BuildTags If a file's name, after stripping the extension and a possible _test suffix, @@ -216,7 +218,7 @@ ToolDir is the directory containing build tools. -## func [ArchChar](/src/target/build.go?s=45435:45479#L1563) +## func [ArchChar](/src/target/build.go?s=47288:47332#L1611) ``` go func ArchChar(goarch string) (string, error) ``` @@ -228,7 +230,7 @@ no longer vary by architecture; they are compile, link, .o, and a.out, respectiv -## func [IsLocalImport](/src/target/build.go?s=44955:44991#L1553) +## func [IsLocalImport](/src/target/build.go?s=46808:46844#L1601) ``` go func IsLocalImport(path string) bool ``` @@ -321,7 +323,7 @@ if set, or else the compiled code's GOARCH, GOOS, and GOROOT. -### func (\*Context) [Import](/src/target/build.go?s=16697:16787#L486) +### func (\*Context) [Import](/src/target/build.go?s=16883:16973#L491) ``` go func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) ``` @@ -345,7 +347,7 @@ If an error occurs, Import returns a non-nil error and a non-nil -### func (\*Context) [ImportDir](/src/target/build.go?s=14860:14937#L434) +### func (\*Context) [ImportDir](/src/target/build.go?s=15046:15123#L439) ``` go func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) ``` @@ -355,7 +357,7 @@ the named directory. -### func (\*Context) [MatchFile](/src/target/build.go?s=30989:31061#L1032) +### func (\*Context) [MatchFile](/src/target/build.go?s=31854:31926#L1050) ``` go func (ctxt *Context) MatchFile(dir, name string) (match bool, err error) ``` @@ -369,7 +371,7 @@ read some or all of the file's content. -### func (\*Context) [SrcDirs](/src/target/build.go?s=7522:7561#L238) +### func (\*Context) [SrcDirs](/src/target/build.go?s=7602:7641#L239) ``` go func (ctxt *Context) SrcDirs() []string ``` @@ -380,7 +382,7 @@ that do not exist. -## type [ImportMode](/src/target/build.go?s=9780:9800#L325) +## type [ImportMode](/src/target/build.go?s=9966:9986#L330) ``` go type ImportMode uint ``` @@ -441,7 +443,7 @@ const ( -## type [MultiplePackageError](/src/target/build.go?s=15413:15621#L451) +## type [MultiplePackageError](/src/target/build.go?s=15599:15807#L456) ``` go type MultiplePackageError struct { Dir string // directory containing files @@ -461,14 +463,14 @@ multiple buildable Go source files for multiple packages. -### func (\*MultiplePackageError) [Error](/src/target/build.go?s=15623:15668#L457) +### func (\*MultiplePackageError) [Error](/src/target/build.go?s=15809:15854#L462) ``` go func (e *MultiplePackageError) Error() string ``` -## type [NoGoError](/src/target/build.go?s=15165:15202#L441) +## type [NoGoError](/src/target/build.go?s=15351:15388#L446) ``` go type NoGoError struct { Dir string @@ -487,14 +489,14 @@ test files, files hidden by build tags, and so on.) -### func (\*NoGoError) [Error](/src/target/build.go?s=15204:15238#L445) +### func (\*NoGoError) [Error](/src/target/build.go?s=15390:15424#L450) ``` go func (e *NoGoError) Error() string ``` -## type [Package](/src/target/build.go?s=11686:14547#L372) +## type [Package](/src/target/build.go?s=11872:14733#L377) ``` go type Package struct { Dir string // directory containing package sources @@ -557,14 +559,14 @@ A Package describes the Go package found in a directory. -### func [Import](/src/target/build.go?s=33366:33433#L1117) +### func [Import](/src/target/build.go?s=34178:34245#L1135) ``` go func Import(path, srcDir string, mode ImportMode) (*Package, error) ``` Import is shorthand for Default.Import. -### func [ImportDir](/src/target/build.go?s=33531:33592#L1122) +### func [ImportDir](/src/target/build.go?s=34343:34404#L1140) ``` go func ImportDir(dir string, mode ImportMode) (*Package, error) ``` @@ -574,7 +576,7 @@ ImportDir is shorthand for Default.ImportDir. -### func (\*Package) [IsCommand](/src/target/build.go?s=14705:14739#L428) +### func (\*Package) [IsCommand](/src/target/build.go?s=14891:14925#L433) ``` go func (p *Package) IsCommand() bool ``` diff --git a/examples/errors/README.md b/examples/errors/README.md new file mode 100644 index 0000000..179f238 --- /dev/null +++ b/examples/errors/README.md @@ -0,0 +1,544 @@ + + +# errors +`import "github.com/pkg/errors"` + +* [Overview](#pkg-overview) +* [Index](#pkg-index) +* [Examples](#pkg-examples) + +## Overview +Package errors provides simple error handling primitives. + +The traditional error handling idiom in Go is roughly akin to + + + if err != nil { + return err + } + +which applied recursively up the call stack results in error reports +without context or debugging information. The errors package allows +programmers to add context to the failure path in their code in a way +that does not destroy the original value of the error. + +### Adding context to an error +The errors.Wrap function returns a new error that adds context to the +original error by recording a stack trace at the point Wrap is called, +and the supplied message. For example + + + _, err := ioutil.ReadAll(r) + if err != nil { + return errors.Wrap(err, "read failed") + } + +If additional control is required the errors.WithStack and errors.WithMessage +functions destructure errors.Wrap into its component operations of annotating +an error with a stack trace and an a message, respectively. + +### Retrieving the cause of an error +Using errors.Wrap constructs a stack of errors, adding context to the +preceding error. Depending on the nature of the error it may be necessary +to reverse the operation of errors.Wrap to retrieve the original error +for inspection. Any error value which implements this interface + + + type causer interface { + Cause() error + } + +can be inspected by errors.Cause. errors.Cause will recursively retrieve +the topmost error which does not implement causer, which is assumed to be +the original cause. For example: + + + switch err := errors.Cause(err).(type) { + case *MyError: + // handle specifically + default: + // unknown error + } + +causer interface is not exported by this package, but is considered a part +of stable public API. + +### Formatted printing of errors +All error values returned from this package implement fmt.Formatter and can +be formatted by the fmt package. The following verbs are supported + + + %s print the error. If the error has a Cause it will be + printed recursively + %v see %s + %+v extended format. Each Frame of the error's StackTrace will + be printed in detail. + +### Retrieving the stack trace of an error or wrapper +New, Errorf, Wrap, and Wrapf record a stack trace at the point they are +invoked. This information can be retrieved with the following interface. + + + type stackTracer interface { + StackTrace() errors.StackTrace + } + +Where errors.StackTrace is defined as + + + type StackTrace []Frame + +The Frame type represents a call site in the stack trace. Frame supports +the fmt.Formatter interface that can be used for printing information about +the stack trace of this error. For example: + + + if err, ok := err.(stackTracer); ok { + for _, f := range err.StackTrace() { + fmt.Printf("%+s:%d", f) + } + } + +stackTracer interface is not exported by this package, but is considered a part +of stable public API. + +See the documentation for Frame.Format for more details. + + + +#### Example (StackTrace) + +Code: +``` go +type stackTracer interface { + StackTrace() errors.StackTrace +} + +err, ok := errors.Cause(fn()).(stackTracer) +if !ok { + panic("oops, err does not implement stackTracer") +} + +st := err.StackTrace() +fmt.Printf("%+v", st[0:2]) // top two frames + +// Example output: +// github.com/pkg/errors_test.fn +// /home/dfc/src/github.com/pkg/errors/example_test.go:47 +// github.com/pkg/errors_test.Example_stackTrace +// /home/dfc/src/github.com/pkg/errors/example_test.go:127 +``` + + +## Index +* [func Cause(err error) error](#Cause) +* [func Errorf(format string, args ...interface{}) error](#Errorf) +* [func New(message string) error](#New) +* [func WithMessage(err error, message string) error](#WithMessage) +* [func WithStack(err error) error](#WithStack) +* [func Wrap(err error, message string) error](#Wrap) +* [func Wrapf(err error, format string, args ...interface{}) error](#Wrapf) +* [type Frame](#Frame) + * [func (f Frame) Format(s fmt.State, verb rune)](#Frame.Format) +* [type StackTrace](#StackTrace) + * [func (st StackTrace) Format(s fmt.State, verb rune)](#StackTrace.Format) + +#### Examples +* [Package (StackTrace)](#example__stackTrace) +* [Cause](#example_Cause) +* [Cause (Printf)](#example_Cause_printf) +* [Errorf (Extended)](#example_Errorf_extended) +* [New](#example_New) +* [New (Printf)](#example_New_printf) +* [WithMessage](#example_WithMessage) +* [WithStack](#example_WithStack) +* [WithStack (Printf)](#example_WithStack_printf) +* [Wrap](#example_Wrap) +* [Wrap (Extended)](#example_Wrap_extended) +* [Wrapf](#example_Wrapf) + +#### Package files +[errors.go](/src/github.com/pkg/errors/errors.go) [stack.go](/src/github.com/pkg/errors/stack.go) + + + + + +## func [Cause](/src/target/errors.go?s=6654:6681#L256) +``` go +func Cause(err error) error +``` +Cause returns the underlying cause of the error, if possible. +An error value has a cause if it implements the following +interface: + + + type causer interface { + Cause() error + } + +If the error does not implement Cause, the original error will +be returned. If the error is nil, nil will be returned without further +investigation. + + + +#### Example + +Code: +``` go +err := fn() +fmt.Println(err) +fmt.Println(errors.Cause(err)) +``` +Output: + + outer: middle: inner: error + error + + + +#### Example (Printf) + +Code: +``` go +err := errors.Wrap(func() error { + return func() error { + return errors.Errorf("hello %s", fmt.Sprintf("world")) + }() +}(), "failed") + +fmt.Printf("%v", err) +``` +Output: + + failed: hello world + + + +## func [Errorf](/src/target/errors.go?s=3695:3748#L111) +``` go +func Errorf(format string, args ...interface{}) error +``` +Errorf formats according to a format specifier and returns the string +as a value that satisfies error. +Errorf also records the stack trace at the point it was called. + + + +#### Example (Extended) + +Code: +``` go +err := errors.Errorf("whoops: %s", "foo") +fmt.Printf("%+v", err) + +// Example output: +// whoops: foo +// github.com/pkg/errors_test.ExampleErrorf +// /home/dfc/src/github.com/pkg/errors/example_test.go:101 +// testing.runExample +// /home/dfc/go/src/testing/example.go:114 +// testing.RunExamples +// /home/dfc/go/src/testing/example.go:38 +// testing.(*M).Run +// /home/dfc/go/src/testing/testing.go:744 +// main.main +// /github.com/pkg/errors/_test/_testmain.go:102 +// runtime.main +// /home/dfc/go/src/runtime/proc.go:183 +// runtime.goexit +// /home/dfc/go/src/runtime/asm_amd64.s:2059 +``` + +## func [New](/src/target/errors.go?s=3420:3450#L101) +``` go +func New(message string) error +``` +New returns an error with the supplied message. +New also records the stack trace at the point it was called. + + + +#### Example + +Code: +``` go +err := errors.New("whoops") +fmt.Println(err) +``` +Output: + + whoops + + + +#### Example (Printf) + +Code: +``` go +err := errors.New("whoops") +fmt.Printf("%+v", err) + +// Example output: +// whoops +// github.com/pkg/errors_test.ExampleNew_printf +// /home/dfc/src/github.com/pkg/errors/example_test.go:17 +// testing.runExample +// /home/dfc/go/src/testing/example.go:114 +// testing.RunExamples +// /home/dfc/go/src/testing/example.go:38 +// testing.(*M).Run +// /home/dfc/go/src/testing/testing.go:744 +// main.main +// /github.com/pkg/errors/_test/_testmain.go:106 +// runtime.main +// /home/dfc/go/src/runtime/proc.go:183 +// runtime.goexit +// /home/dfc/go/src/runtime/asm_amd64.s:2059 +``` + +## func [WithMessage](/src/target/errors.go?s=5698:5747#L213) +``` go +func WithMessage(err error, message string) error +``` +WithMessage annotates err with a new message. +If err is nil, WithMessage returns nil. + + + +#### Example + +Code: +``` go +cause := errors.New("whoops") +err := errors.WithMessage(cause, "oh noes") +fmt.Println(err) +``` +Output: + + oh noes: whoops + + + +## func [WithStack](/src/target/errors.go?s=4406:4437#L144) +``` go +func WithStack(err error) error +``` +WithStack annotates err with a stack trace at the point WithStack was called. +If err is nil, WithStack returns nil. + + + +#### Example + +Code: +``` go +cause := errors.New("whoops") +err := errors.WithStack(cause) +fmt.Println(err) +``` +Output: + + whoops + + + +#### Example (Printf) + +Code: +``` go +cause := errors.New("whoops") +err := errors.WithStack(cause) +fmt.Printf("%+v", err) + +// Example Output: +// whoops +// github.com/pkg/errors_test.ExampleWithStack_printf +// /home/fabstu/go/src/github.com/pkg/errors/example_test.go:55 +// testing.runExample +// /usr/lib/go/src/testing/example.go:114 +// testing.RunExamples +// /usr/lib/go/src/testing/example.go:38 +// testing.(*M).Run +// /usr/lib/go/src/testing/testing.go:744 +// main.main +// github.com/pkg/errors/_test/_testmain.go:106 +// runtime.main +// /usr/lib/go/src/runtime/proc.go:183 +// runtime.goexit +// /usr/lib/go/src/runtime/asm_amd64.s:2086 +// github.com/pkg/errors_test.ExampleWithStack_printf +// /home/fabstu/go/src/github.com/pkg/errors/example_test.go:56 +// testing.runExample +// /usr/lib/go/src/testing/example.go:114 +// testing.RunExamples +// /usr/lib/go/src/testing/example.go:38 +// testing.(*M).Run +// /usr/lib/go/src/testing/testing.go:744 +// main.main +// github.com/pkg/errors/_test/_testmain.go:106 +// runtime.main +// /usr/lib/go/src/runtime/proc.go:183 +// runtime.goexit +// /usr/lib/go/src/runtime/asm_amd64.s:2086 +``` + +## func [Wrap](/src/target/errors.go?s=5050:5092#L180) +``` go +func Wrap(err error, message string) error +``` +Wrap returns an error annotating err with a stack trace +at the point Wrap is called, and the supplied message. +If err is nil, Wrap returns nil. + + + +#### Example + +Code: +``` go +cause := errors.New("whoops") +err := errors.Wrap(cause, "oh noes") +fmt.Println(err) +``` +Output: + + oh noes: whoops + + + +#### Example (Extended) + +Code: +``` go +err := fn() +fmt.Printf("%+v\n", err) + +// Example output: +// error +// github.com/pkg/errors_test.fn +// /home/dfc/src/github.com/pkg/errors/example_test.go:47 +// github.com/pkg/errors_test.ExampleCause_printf +// /home/dfc/src/github.com/pkg/errors/example_test.go:63 +// testing.runExample +// /home/dfc/go/src/testing/example.go:114 +// testing.RunExamples +// /home/dfc/go/src/testing/example.go:38 +// testing.(*M).Run +// /home/dfc/go/src/testing/testing.go:744 +// main.main +// /github.com/pkg/errors/_test/_testmain.go:104 +// runtime.main +// /home/dfc/go/src/runtime/proc.go:183 +// runtime.goexit +// /home/dfc/go/src/runtime/asm_amd64.s:2059 +// github.com/pkg/errors_test.fn +// /home/dfc/src/github.com/pkg/errors/example_test.go:48: inner +// github.com/pkg/errors_test.fn +// /home/dfc/src/github.com/pkg/errors/example_test.go:49: middle +// github.com/pkg/errors_test.fn +// /home/dfc/src/github.com/pkg/errors/example_test.go:50: outer +``` + +## func [Wrapf](/src/target/errors.go?s=5384:5447#L197) +``` go +func Wrapf(err error, format string, args ...interface{}) error +``` +Wrapf returns an error annotating err with a stack trace +at the point Wrapf is call, and the format specifier. +If err is nil, Wrapf returns nil. + + + +#### Example + +Code: +``` go +cause := errors.New("whoops") +err := errors.Wrapf(cause, "oh noes #%d", 2) +fmt.Println(err) +``` +Output: + + oh noes #2: whoops + + + + +## type [Frame](/src/target/stack.go?s=131:149#L12) +``` go +type Frame uintptr +``` +Frame represents a program counter inside a stack frame. + + + + + + + + + + +### func (Frame) [Format](/src/target/stack.go?s=1204:1249#L52) +``` go +func (f Frame) Format(s fmt.State, verb rune) +``` +Format formats the frame according to the fmt.Formatter interface. + + + %s source file + %d source line + %n function name + %v equivalent to %s:%d + +Format accepts flags that alter the printing of some verbs, as follows: + + + %+s function name and path of source file relative to the compile time + GOPATH separated by \n\t (\n\t) + %+v equivalent to %+s:%d + + + + +## type [StackTrace](/src/target/stack.go?s=1854:1877#L81) +``` go +type StackTrace []Frame +``` +StackTrace is stack of Frames from innermost (newest) to outermost (oldest). + + + + + + + + + + +### func (StackTrace) [Format](/src/target/stack.go?s=2258:2309#L91) +``` go +func (st StackTrace) Format(s fmt.State, verb rune) +``` +Format formats the stack of Frames according to the fmt.Formatter interface. + + + %s lists source files for each Frame in the stack + %v lists the source file and line number for each Frame in the stack + +Format accepts flags that alter the printing of some verbs, as follows: + + + %+v Prints filename, function, and line number for each Frame in the stack. + + + + + + + + +- - - +Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) diff --git a/examples/fs/README.md b/examples/fs/README.md index 8c5ce6a..feeff02 100644 --- a/examples/fs/README.md +++ b/examples/fs/README.md @@ -84,6 +84,20 @@ Walker does not follow symbolic links. +#### Example + +Code: +``` go +walker := fs.Walk("/usr/lib") +for walker.Step() { + if err := walker.Err(); err != nil { + fmt.Fprintln(os.Stderr, err) + continue + } + fmt.Println(walker.Path()) +} +``` + diff --git a/examples/martini/README.md b/examples/martini/README.md index dabb8ae..bf13d98 100644 --- a/examples/martini/README.md +++ b/examples/martini/README.md @@ -5,7 +5,6 @@ * [Overview](#pkg-overview) * [Index](#pkg-index) -* [Subdirectories](#pkg-subdirectories) ## Overview Package martini is a powerful package for quickly writing modular web applications/services in Golang. diff --git a/examples/sessions/README.md b/examples/sessions/README.md index 7107a93..354ba7a 100644 --- a/examples/sessions/README.md +++ b/examples/sessions/README.md @@ -87,7 +87,7 @@ flashes, call session.Flashes(). Here is an example: return } - // Get the previously flashes, if any. + // Get the previous flashes, if any. if flashes := session.Flashes(); len(flashes) > 0 { // Use the flash values. } else { @@ -251,7 +251,7 @@ session to a common registry. Save() uses it to save all registered sessions. -## func [NewCookie](/src/target/sessions.go?s=5420:5485#L195) +## func [NewCookie](/src/target/sessions.go?s=5493:5558#L197) ``` go func NewCookie(name, value string, options *Options) *http.Cookie ``` @@ -261,7 +261,7 @@ Explorer compatibility. -## func [Save](/src/target/sessions.go?s=5158:5213#L188) +## func [Save](/src/target/sessions.go?s=5231:5286#L190) ``` go func Save(r *http.Request, w http.ResponseWriter) error ``` @@ -452,7 +452,7 @@ web browser. -## type [MultiError](/src/target/sessions.go?s=6092:6115#L220) +## type [MultiError](/src/target/sessions.go?s=6165:6188#L222) ``` go type MultiError []error ``` @@ -469,20 +469,21 @@ Borrowed from the App Engine SDK. -### func (MultiError) [Error](/src/target/sessions.go?s=6117:6151#L222) +### func (MultiError) [Error](/src/target/sessions.go?s=6190:6224#L224) ``` go func (m MultiError) Error() string ``` -## type [Options](/src/target/sessions.go?s=516:798#L24) +## type [Options](/src/target/sessions.go?s=516:843#L24) ``` go type Options struct { Path string Domain string - // MaxAge=0 means no 'Max-Age' attribute specified. - // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'. + // MaxAge=0 means no Max-Age attribute specified and the cookie will be + // deleted after the browser session ends. + // MaxAge<0 means delete cookie immediately. // MaxAge>0 means Max-Age attribute present and given in seconds. MaxAge int Secure bool @@ -502,7 +503,7 @@ Fields are a subset of http.Cookie fields. -## type [Registry](/src/target/sessions.go?s=3728:3809#L139) +## type [Registry](/src/target/sessions.go?s=3801:3882#L141) ``` go type Registry struct { // contains filtered or unexported fields @@ -516,7 +517,7 @@ Registry stores sessions used during a request. -### func [GetRegistry](/src/target/sessions.go?s=3383:3426#L125) +### func [GetRegistry](/src/target/sessions.go?s=3456:3499#L127) ``` go func GetRegistry(r *http.Request) *Registry ``` @@ -526,7 +527,7 @@ GetRegistry returns a registry instance for the current request. -### func (\*Registry) [Get](/src/target/sessions.go?s=3969:4047#L147) +### func (\*Registry) [Get](/src/target/sessions.go?s=4042:4120#L149) ``` go func (s *Registry) Get(store Store, name string) (session *Session, err error) ``` @@ -537,7 +538,7 @@ It returns a new session if there are no sessions registered for the name. -### func (\*Registry) [Save](/src/target/sessions.go?s=4465:4517#L163) +### func (\*Registry) [Save](/src/target/sessions.go?s=4538:4590#L165) ``` go func (s *Registry) Save(w http.ResponseWriter) error ``` @@ -546,7 +547,7 @@ Save saves all sessions registered for the current request. -## type [Session](/src/target/sessions.go?s=1183:1457#L47) +## type [Session](/src/target/sessions.go?s=1256:1530#L49) ``` go type Session struct { // The ID of the session, generated by stores. It should not be used for @@ -567,7 +568,7 @@ Session stores the values and optional configuration for a session. -### func [NewSession](/src/target/sessions.go?s=957:1007#L38) +### func [NewSession](/src/target/sessions.go?s=1002:1052#L39) ``` go func NewSession(store Store, name string) *Session ``` @@ -577,7 +578,7 @@ NewSession is called by session stores to create a new session instance. -### func (\*Session) [AddFlash](/src/target/sessions.go?s=2138:2199#L81) +### func (\*Session) [AddFlash](/src/target/sessions.go?s=2211:2272#L83) ``` go func (s *Session) AddFlash(value interface{}, vars ...string) ``` @@ -589,7 +590,7 @@ the flash key. If not defined "_flash" is used by default. -### func (\*Session) [Flashes](/src/target/sessions.go?s=1661:1716#L63) +### func (\*Session) [Flashes](/src/target/sessions.go?s=1734:1789#L65) ``` go func (s *Session) Flashes(vars ...string) []interface{} ``` @@ -601,7 +602,7 @@ the flash key. If not defined "_flash" is used by default. -### func (\*Session) [Name](/src/target/sessions.go?s=2764:2795#L101) +### func (\*Session) [Name](/src/target/sessions.go?s=2837:2868#L103) ``` go func (s *Session) Name() string ``` @@ -610,7 +611,7 @@ Name returns the name used to register the session. -### func (\*Session) [Save](/src/target/sessions.go?s=2605:2673#L96) +### func (\*Session) [Save](/src/target/sessions.go?s=2678:2746#L98) ``` go func (s *Session) Save(r *http.Request, w http.ResponseWriter) error ``` @@ -621,7 +622,7 @@ the response or returning from the handler. -### func (\*Session) [Store](/src/target/sessions.go?s=2881:2912#L106) +### func (\*Session) [Store](/src/target/sessions.go?s=2954:2985#L108) ``` go func (s *Session) Store() Store ``` diff --git a/main.go b/main.go index e72b902..0e9fdb2 100644 --- a/main.go +++ b/main.go @@ -61,12 +61,15 @@ var ( fs = vfs.NameSpace{} funcs = map[string]interface{}{ - "comment_md": commentMdFunc, - "base": path.Base, - "md": mdFunc, - "pre": preFunc, - "kebab": kebabFunc, - "bitscape": bitscapeFunc, //Escape [] for bitbucket confusion + "comment_md": commentMdFunc, + "base": path.Base, + "md": mdFunc, + "pre": preFunc, + "kebab": kebabFunc, + "bitscape": bitscapeFunc, //Escape [] for bitbucket confusion + "show_examples": func() bool { return *showExamples }, + "examples": examplesFunc, + "output": outputFunc, } ) @@ -86,6 +89,14 @@ func preFunc(text string) string { return "``` go\n" + text + "\n```" } +func outputFunc(output string) string { + lines := strings.Split(output, "\n") + for i, line := range lines { + lines[i] = " " + line + } + return "\n" + strings.Join(lines, "\n") +} + // Original Source https://github.com/golang/tools/blob/master/godoc/godoc.go#L562 func srcLinkFunc(s string) string { s = path.Clean("/" + s) diff --git a/template.go b/template.go index 7dd6037..cb4f917 100644 --- a/template.go +++ b/template.go @@ -1,6 +1,14 @@ package main -var pkgTemplate = `{{with .PDoc}} +var pkgTemplate = `{{define "examples_md"}}{{range .}} +#### Example{{example_suffix .Name}} +{{comment_md .Doc}} +Code: +{{pre .Code}}{{if .Output}} +Output: +{{output .Output}} +{{end}} +{{end}}{{end}}{{with .PDoc}} {{if $.IsMain}} > {{ base .ImportPath }} {{comment_md .Doc}} @@ -9,13 +17,13 @@ var pkgTemplate = `{{with .PDoc}} ` + "`" + `import "{{.ImportPath}}"` + "`" + ` * [Overview](#pkg-overview) -* [Index](#pkg-index){{if $.Examples}} +* [Index](#pkg-index){{if and show_examples $.Examples}} * [Examples](#pkg-examples){{- end}}{{if $.Dirs}} * [Subdirectories](#pkg-subdirectories){{- end}} ## Overview {{comment_md .Doc}} -{{example_html $ ""}} +{{template "examples_md" (examples $ "")}} ## Index{{if .Consts}} * [Constants](#pkg-constants){{end}}{{if .Vars}} @@ -25,8 +33,8 @@ var pkgTemplate = `{{with .PDoc}} * [{{node_html $ .Decl false | sanitize}}](#{{$name_html}}){{- end}}{{- range .Methods}}{{$name_html := html .Name}} * [{{node_html $ .Decl false | sanitize}}](#{{$tname_html}}.{{$name_html}}){{- end}}{{- end}}{{- if $.Notes}}{{- range $marker, $item := $.Notes}} * [{{noteTitle $marker | html}}s](#pkg-note-{{$marker}}){{end}}{{end}} -{{if $.Examples}} -#### Examples{{- range $.Examples}} +{{if and show_examples $.Examples}} +#### Examples{{- range examples $ "*" }} * [{{example_name .Name}}](#example_{{.Name}}){{- end}}{{- end}} {{with .Filenames}} #### Package files @@ -43,7 +51,7 @@ var pkgTemplate = `{{with .PDoc}} {{range .Funcs}}{{$name_html := html .Name}}## func [{{$name_html}}]({{posLink_url $ .Decl}}) {{node $ .Decl | pre}} {{comment_md .Doc}} -{{example_html $ .Name}} +{{template "examples_md" (examples $ .Name)}} {{callgraph_html $ "" .Name}}{{end}} {{range .Types}}{{$tname := .Name}}{{$tname_html := html .Name}}## type [{{$tname_html}}]({{posLink_url $ .Decl}}) {{node $ .Decl | pre}} @@ -53,20 +61,20 @@ var pkgTemplate = `{{with .PDoc}} {{node $ .Decl | pre }} {{comment_md .Doc}}{{end}} -{{example_html $ $tname}} +{{template "examples_md" (examples $ $tname)}} {{implements_html $ $tname}} {{methodset_html $ $tname}} {{range .Funcs}}{{$name_html := html .Name}}### func [{{$name_html}}]({{posLink_url $ .Decl}}) {{node $ .Decl | pre}} {{comment_md .Doc}} -{{example_html $ .Name}}{{end}} +{{template "examples_md" (examples $ .Name)}}{{end}} {{callgraph_html $ "" .Name}} {{range .Methods}}{{$name_html := html .Name}}### func ({{md .Recv}}) [{{$name_html}}]({{posLink_url $ .Decl}}) {{node $ .Decl | pre}} {{comment_md .Doc}} -{{$name := printf "%s_%s" $tname .Name}}{{example_html $ $name}} +{{$name := printf "%s_%s" $tname .Name}}{{template "examples_md" (examples $ $name)}} {{callgraph_html $ .Recv .Name}} {{end}}{{end}}{{end}}