使用多个" \ n"的字符串变量的相当格式化在GO代码中

时间:2018-02-01 16:43:49

标签: string go

我是GO代码的新手,并且在使用多个\n(回车)格式化字符串变量时遇到了一些问题。我有一个字符串变量存储在其中:

  

" 1。检查连接\ n2。已检查防火墙设置\ n3。检查交换机/网络连接\ n4。联系管理员"

现在,我想要打印该行,使其看起来像这样:

Remedy:                  1.  Check the connections
                         2.  Have the firewall setting been checked
                         3.  Check switch/network connections
                         4.  Contact admin

但是,在我运行输出行的程序中:

fmt.Println("remedy:\t\t\t\t" + alt.Remedy)

它出现了这样:

remedy:                         1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin

如何通过发出该命令来获取它,所有4个选项都列在同一列中?在这个例子中,我想要在#1选项下列出选项2,3和4。

提前致谢!

4 个答案:

答案 0 :(得分:2)

  

The Go Programming Language Specification

     

String literals

     

字符串文字表示从中获取的字符串常量   连接一系列字符。有两种形式:原始   字符串文字和解释的字符串文字。

     

原始字符串文字是后引号之间的字符序列,如   foo。在引号内,除反向引号外,任何字符都可能出现。   原始字符串文字的值是由字符串组成的字符串   未解释的(隐式UTF-8编码)字符之间   报价;特别是,反斜杠没有特殊含义   字符串可能包含换行符。里面有回车符(' \ r')   原始字符串文字将从原始字符串值中丢弃。

使用raw string literal。例如,

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:])
}

游乐场:https://play.golang.org/p/pdgAoYnBIch

输出:

Remedy:                  1.  Check the connections
                         2.  Have the firewall setting been checked
                         3.  Check switch/network connections
                         4.  Contact admin

答案 1 :(得分:0)

Go中没有任何东西可以为你做这件事。

相反,您可以将字符串拆分为行并相应地格式化它们。

以下是一个例子:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "1. Check the connections \n2. Have the firewall settings been checked \n3. Check switch/network connections \n4. Contact admin"
    // Split the string into lines.
    parts := strings.Split(str, "\n")
    // Iterate over the lines.
    for i, s := range parts {
        if i == 0 {
            // First line: start with "Remedy"
            fmt.Printf("Remedy:\t\t\t%s\n", s)
        } else {
            fmt.Printf("       \t\t\t%s\n", s)
        }
    }
}

输出:

Remedy:         1. Check the connections 
                2. Have the firewall settings been checked 
                3. Check switch/network connections 
                4. Contact admin

注意:可能还有其他一些方法可以做到这一点,我刚选择了这个。

答案 2 :(得分:0)

与Marc的答案类似,但我会这样做。它更简单,更短,更清洁:

使用换行符加上非第一行所需的缩进来替换源string中的每个换行符。您可以使用strings.Replace()

要格式化的源字符串:

s := "1. Check the connections \n2. Have the firewall settings been checked \n3. Check switch/network connections \n4. Contact admin"

格式化很简单:

fmt.Println("Remedy:     ", strings.Replace(s, "\n", "\n             ", -1))

输出(在Go Playground上尝试):

Remedy:      1. Check the connections 
             2. Have the firewall settings been checked 
             3. Check switch/network connections 
             4. Contact admin

答案 3 :(得分:-2)

因为你正在存储"补救措施"在一个字符串中,除了在字符串var中包含间距之外,你还可以做很多事情。

"1. Check the connections\n\t\t\t\t2. Have the firewall settings been checked\n\t\t\t\t3. Check switch/network connections\n\t\t\t\t4. Contact admin"

我建议将它们保存为切片并迭代打印:

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\n", i+1, remedy[i])
            continue
        }
        fmt.Printf("%20d. %s\n", i+1, remedy[i])
    }
}

Example on play