当前位置: > > > > Golang:试图找出为什么“\n”不重要
Golang:试图找出为什么“\n”不重要
来源:stackoverflow
2024-04-27 12:09:37
0浏览
收藏
大家好,今天本人给大家带来文章《Golang:试图找出为什么“\n”不重要》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容
作为我学校的一个项目,我们需要制作一个 ascii 艺术文本版本。我的意思是使用这个命令:
快跑吧。 “你好世界!”标准 –output=”banner.txt”
其中的标准是用于 ascii 艺术的figlet,以及将结果放入文件中的输出,调用banner.txt。
现在,(我的代码非常难看)我得到了这个:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
if len(os.Args) == 4 {
args := os.Args
arg3 := os.Args[3]
arg4 := strings.Split(arg3, "=")
if arg4[0] == "--output" {
file, err := os.Create(arg4[1])
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
police := ""
if args != nil && len(args) > 1 {
arg2 := os.Args[2]
if arg2 == "standard" {
police = "standard.txt"
} else if arg2 == "shadow" {
police = "shadow.txt"
} else if arg2 == "thinkertoy" {
police = "thinkertoy.txt"
} else if arg2 == "graffiti" {
police = "graffiti.txt"
} else {
fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
fmt.Println(" Figlet name invalid. Available : ")
fmt.Println(" - graffiti")
fmt.Println(" - standard")
fmt.Println(" - shadow")
fmt.Println(" - thinkertoy")
}
}
fichier, err := os.Open(police)
if err != nil {
fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
return
}
scanner := bufio.NewScanner(fichier) // Scan le fichier
scanner.Split(bufio.ScanLines) // split le scan en lignes
var lignes []string // englobe toutes les lignes
for scanner.Scan() {
lignes = append(lignes, scanner.Text())
}
asciiChrs := make(map[int][]string) // mise en place de la map (vide pour l'instant)
dec := 31 // correspond a la case zero de la map
for _, ligne := range lignes { //ligne = une/chaques ligne dans l'intervalle lignes du fichier txt
if ligne == "" {
dec++
} else {
asciiChrs[dec] = append(asciiChrs[dec], ligne)
}
}
userInput := os.Args[1]
invCharCount := 0
for _, invalidChar := range userInput {
if invalidChar < 31 || invalidChar > 126 {
invCharCount++
fmt.Println("Caractère non supporté: ", string(invalidChar))
}
}
if invCharCount > 0 {
fmt.Println("Try Again")
os.Exit(0)
} else if userInput == "\\n" {
fmt.Print("\n")
return
} else if userInput == "" {
return
} else {
Newline(userInput, asciiChrs)
}
} else {
fmt.Println("Usage: go run . [STRING] [BANNER]\n\nEX: go run . something standard")
}
}
}
func Newline(n string, y map[int][]string) {
replaceNewline := strings.Replace(n, "\\n", "\n", -1)
wordsSlice := strings.Split(replaceNewline, "\n")
slice := []string{}
slice2 := []string{}
slice3 := []string{}
slice4 := []string{}
slice5 := []string{}
slice6 := []string{}
slice7 := []string{}
slice8 := []string{}
for _, mot := range wordsSlice {
//for j := 0; j < 8; /*lignes*/ j++ {
for _, lettre := range mot {
//fmt.Print(y[int(lettre)][j])
slice = append(slice, (y[int(lettre)][0]))
slice2 = append(slice2, (y[int(lettre)][1]))
slice3 = append(slice3, (y[int(lettre)][2]))
slice4 = append(slice4, (y[int(lettre)][3]))
slice5 = append(slice5, (y[int(lettre)][4]))
slice6 = append(slice6, (y[int(lettre)][5]))
slice7 = append(slice7, (y[int(lettre)][6]))
slice8 = append(slice8, (y[int(lettre)][7]))
}
fmt.Println()
// }
}
f, err := os.OpenFile("banner.txt", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
outpout := strings.Join(slice, "")
outpout2 := strings.Join(slice2, "")
outpout3 := strings.Join(slice3, "")
outpout4 := strings.Join(slice4, "")
outpout5 := strings.Join(slice5, "")
outpout6 := strings.Join(slice6, "")
outpout7 := strings.Join(slice7, "")
outpout8 := strings.Join(slice8, "")
if _, err = f.WriteString(outpout); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout2); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout3); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout4); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout5); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout6); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout7); err != nil {
panic(err)
}
if _, err = f.WriteString("\n"); err != nil {
panic(err)
}
if _, err = f.WriteString(outpout8); err != nil {
panic(err)
}
}
所以,我最大的问题是每当我输入这样的内容时:
快跑吧。 “你好\n那里”标准 –output=”banner.txt”
\n 不算作 \n 并且不执行任何操作。
我希望有人能帮助我!如果您有任何问题请告诉我:)
正确答案
这有点棘手,因为 shell 不解释转义序列(正如 peter 在他的评论中所澄清的那样)。
解决这个问题的方法是将参数作为 \\n 传递,例如:
go run . "hello\\nthere" standard --output="banner.txt"
然后,在您的代码中,您可以对需要考虑换行符的每个参数使用 strings 包中的 replace 函数,如下所示:
output := strings.Replace(os.Args[1], "\\n", "\n", -1)
这不是一个超级干净的解决方案,但它可以完成工作。
本篇关于《Golang:试图找出为什么“\n”不重要》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注米云公众号!
