I am new to GO code and am having some issues formatting string variables with multiple (carriage returns) in it. I have a string variable that has stored in it the following:
"1. Check the connections 2. Have the firewall settings been checked 3. Check switch/network connections 4. Contact admin"
Now, I want to print the line so that it looks like this:
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
However, in my program when I run the output line:
fmt.Println("remedy:\t\t\t\t" + alt.Remedy)
It comes up like this:
remedy: 1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin
How do I get it so that by issuing that command, all 4 options are listed in the same column? In this example, I want options 2, 3, and 4 listed under the #1 option.
Thanks in advance!
There's nothing in Go to do this for you.
Instead, you can split the string into lines and format them accordingly.
Here is an example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin"
// Split the string into lines.
parts := strings.Split(str, "
")
// Iterate over the lines.
for i, s := range parts {
if i == 0 {
// First line: start with "Remedy"
fmt.Printf("Remedy:\t\t\t%s
", s)
} else {
fmt.Printf(" \t\t\t%s
", s)
}
}
}
This outputs:
Remedy: 1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin
Note: there are probably a few other ways to do this, I just picked this one.
Since you are storing the "remedies" together in a string, there isn't much you can do other than include spacing in your string var.
"1. Check the connections
\t\t\t\t2. Have the firewall settings been checked
\t\t\t\t3. Check switch/network connections
\t\t\t\t4. Contact admin"
I would suggest saving them as a slice and iterating to print:
package main
import "fmt"
var remedy = []string{
"Check the connections",
"Have the firewall settings been checked",
"Check switch/network connections",
"Contact admin",
}
func main() {
fmt.Print("remedy:")
for i := range remedy {
if i == 0 {
fmt.Printf("%13d. %s
", i+1, remedy[i])
continue
}
fmt.Printf("%20d. %s
", i+1, remedy[i])
}
}
The Go Programming Language Specification
A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals.
Raw string literals are character sequences between back quotes, as in
foo
. Within the quotes, any character may appear except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines. Carriage return characters ('') inside raw string literals are discarded from the raw string value.
Use a raw string literal. For example,
package main
import (
"fmt"
)
func main() {
message := `
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
`
fmt.Print(message[1:])
}
Playground: https://play.golang.org/p/pdgAoYnBIch
Output:
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
Similar to Marc's answer, but I would do it this way. It's simpler, shorter and cleaner:
Replace each newline character in the source string
with a newline plus the indentation that non-first lines need. You may use strings.Replace()
for this.
The source string to be formatted:
s := "1. Check the connections 2. Have the firewall settings been checked 3. Check switch/network connections 4. Contact admin"
And the formatting is a simple line:
fmt.Println("Remedy: ", strings.Replace(s, "
", "
", -1))
Output (try it on the Go Playground):
Remedy: 1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin