func warn() {
fmt.Fprintln(os.Stderr, "┌───────────────────────────┐")
fmt.Fprintln(os.Stderr, "│ │")
fmt.Fprintln(os.Stderr, "│ warning! Run with caution │")
fmt.Fprintln(os.Stderr, "│ warning! Run with caution │")
fmt.Fprintln(os.Stderr, "│ │")
fmt.Fprintln(os.Stderr, "└───────────────────────────┘")
// but i want use a single call to `fmt.Fprint`
// like
msg := `
┌───────────────────────────┐
│ │
│ warning! Run with caution │
│ warning! Run with caution │
│ │
└───────────────────────────┘
`
fmt.Fprint(os.Stderr, msg)
// but i can't get rid of the first newline
// i can place the ` beginning of the line
msg =
`┌───────────────────────────┐
│ │
│ warning! Run with caution │
│ warning! Run with caution │
│ │
└───────────────────────────┘
`
// but that makes it look ugly
// `go fmt` make it more
// after go fmt
msg =
`┌───────────────────────────┐
│ │
│ warning! Run with caution │
│ warning! Run with caution │
│ │
└───────────────────────────┘
`
}
how can i do this that doesn’t hurt readability and look good, doesn’t need to user more function or create a slice expression.
The problem here is there is a newline in your first line (just before header dashes).
You can do two things: (1) Move your dashed header to line msg:=…
or extract a substring and get rid of newline char, so
runes := []rune(msg) safeSubstring := string(runes[1:])XXXXXXXXXXXXX") fmt.Fprint(os.Stderr, safeSubstring )
that is lot to just to remove the newline.
i want to have as like it will be printed.
No. 1 make the code look ugly
There is no such language construct in Go.
Either you use regular strings without newlines and a lot of escaping, or you use raw strings, which are really that, raw. They will be exactly as you put them.
Though you might perhaps use strings.Join()
:
https://play.golang.org/p/Qnfysp4Airo
package main
import (
"fmt"
"strings"
)
func main() {
var (
lines = []string{
"┌───────────────────────────┐",
"│ │",
"│ warning! Run with caution │",
"│ warning! Run with caution │",
"│ │",
"└───────────────────────────┘",
}
block = strings.Join(lines, "\n")
)
fmt.Println(block)
}
raw string needs a good syntax. there is no way to preserve indentation level.
zig has cool approach https://ziglang.org/documentation/master/#Multiline-String-Literals
this has many benefits
Raw string needs to be raw, thats why its called “raw string”. There is no indented multiline string in Go. It is this easy.