heredoc

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2019 License: MIT Imports: 3 Imported by: 1,916

README

heredoc

Build Status GoDoc

About

Package heredoc provides the here-document with keeping indent.

Install

$ go get github.com/MakeNowJust/heredoc

Import

// usual
import "github.com/MakeNowJust/heredoc"

Example

package main

import (
	"fmt"
	"github.com/MakeNowJust/heredoc"
)

func main() {
	fmt.Println(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, ...
	//
}

API Document

License

This software is released under the MIT License, see LICENSE.

Documentation

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"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Doc

func Doc(raw string) string

Doc returns un-indented string as here-document.

Example (Lipsum)
package main

import (
	"fmt"

	"github.com/MakeNowJust/heredoc"
)

func main() {
	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)
package main

import (
	"fmt"

	"github.com/MakeNowJust/heredoc"
)

func main() {
	// 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
package main

import (
	"fmt"

	"github.com/MakeNowJust/heredoc"
)

func main() {
	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

Types

This section is empty.

Directories

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

Jump to

Keyboard shortcuts

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