...

Package heredoc

import "github.com/MakeNowJust/heredoc"
Overview
Index
Examples
Subdirectories

Overview ▾

Package heredoc provides creation of here-documents from raw strings.

Golang supports raw-string syntax.

doc := `
	Foo
	Bar
`

But raw-string cannot recognize indentation. Thus such content is an indented string, equivalent to

"\n\tFoo\n\tBar\n"

I dont't want this!

However this problem is solved by package heredoc.

doc := heredoc.Doc(`
	Foo
	Bar
`)

Is equivalent to

"Foo\nBar\n"

func Doc

func Doc(raw string) string

Doc returns un-indented string as here-document.

Example (Lipsum)

Code:

fmt.Print(heredoc.Doc(`
        Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna
        aliqua. Ut enim ad minim veniam, ...
    `))

Output:

Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, ...

Example (Spec)

Code:

// Single line string is no change.
fmt.Println(heredoc.Doc(`It is single line.`))
// If first line is empty, heredoc.Doc removes first line.
fmt.Println(heredoc.Doc(`
        It is first line.
        It is second line.`))
// If last line is empty and more little length than indents,
// heredoc.Doc removes last line's content.
fmt.Println(heredoc.Doc(`
        Next is last line.
    `))
fmt.Println("Previous is last line.")

Output:

It is single line.
It is first line.
It is second line.
Next is last line.

Previous is last line.

func Docf

func Docf(raw string, args ...interface{}) string

Docf returns unindented and formatted string as here-document. Formatting is done as for fmt.Printf().

Example

Code:

libName := "github.com/MakeNowJust/heredoc"
author := "TSUYUSATO Kitsune (@MakeNowJust)"
fmt.Printf(heredoc.Docf(`
        Library Name  : %s
        Author        : %s
        Repository URL: http://%s.git
    `, libName, author, libName))

Output:

Library Name  : github.com/MakeNowJust/heredoc
Author        : TSUYUSATO Kitsune (@MakeNowJust)
Repository URL: http://github.com/MakeNowJust/heredoc.git

Subdirectories

Name Synopsis
..
dot Package heredoc_dot is the set of shortcuts for dot import.