package jenimport ()// Code represents an item of code that can be rendered.typeCodeinterface { render(f *File, w io.Writer, s *Statement) error isNull(f *File) bool}// Save renders the file and saves to the filename provided.func ( *File) ( string) error {// notest := &bytes.Buffer{}if := .Render(); != nil {return }if := os.WriteFile(, .Bytes(), 0644); != nil {return }returnnil}// Render renders the file to the provided writer.func ( *File) ( io.Writer) error { := &bytes.Buffer{}if := .render(, , nil); != nil {return } := &bytes.Buffer{}iflen(.headers) > 0 {for , := range .headers {if := Comment().render(, , nil); != nil {return }if , := fmt.Fprint(, "\n"); != nil {return } }// Append an extra newline so that header comments don't get lumped in // with package comments.if , := fmt.Fprint(, "\n"); != nil {return } }for , := range .comments {if := Comment().render(, , nil); != nil {return }if , := fmt.Fprint(, "\n"); != nil {return } }if , := fmt.Fprintf(, "package %s", .name); != nil {return }if .CanonicalPath != "" {if , := fmt.Fprintf(, " // import %q", .CanonicalPath); != nil {return } }if , := fmt.Fprint(, "\n\n"); != nil {return }if := .renderImports(); != nil {return }if , := .Write(.Bytes()); != nil {return }var []byteif .NoFormat { = .Bytes() } else {varerror , = format.Source(.Bytes())if != nil {returnfmt.Errorf("Error %s while formatting source:\n%s", , .String()) } }if , := .Write(); != nil {return }returnnil}func ( *File) ( io.Writer) error {// Render the "C" import if it's been used in a `Qual`, `Anon` or if there's a preamble comment := .imports["C"].name != "" || len(.cgoPreamble) > 0// Only separate the import from the main imports block if there's a preamble := && len(.cgoPreamble) > 0 := map[string]importdef{}for , := range .imports {// filter out the "C" pseudo-package so it's not rendered in a block with the other // imports, but only if it is accompanied by a preamble commentif == "C" && {continue } [] = }iflen() == 1 {for , := range {if .alias && != "C" {// "C" package should be rendered without alias even when used as an anonymous import // (e.g. should never have an underscore).if , := fmt.Fprintf(, "import %s %s\n\n", .name, strconv.Quote()); != nil {return } } else {if , := fmt.Fprintf(, "import %s\n\n", strconv.Quote()); != nil {return } } } } elseiflen() > 1 {if , := fmt.Fprint(, "import (\n"); != nil {return }// We must sort the imports to ensure repeatable // source. := []string{}for := range { = append(, ) }sort.Strings()for , := range { := []if .alias && != "C" {// "C" package should be rendered without alias even when used as an anonymous import // (e.g. should never have an underscore).if , := fmt.Fprintf(, "%s %s\n", .name, strconv.Quote()); != nil {return } } else {if , := fmt.Fprintf(, "%s\n", strconv.Quote()); != nil {return } } }if , := fmt.Fprint(, ")\n\n"); != nil {return } }if {for , := range .cgoPreamble {if := Comment().render(, , nil); != nil {return }if , := fmt.Fprint(, "\n"); != nil {return } }if , := fmt.Fprint(, "import \"C\"\n\n"); != nil {return } }returnnil}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.