英文:
Golang : Trying to figure out why "\n" don't count
问题
作为我学校的一个项目,我们需要做一个ASCII艺术文本版本。我的意思是使用以下命令:
go run . "Hello World!" standard --output="banner.txt"
其中,standard是用于ASCII艺术的figlet字体,output参数用于将结果输出到名为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)
}
}
所以,我最大的问题是,当我输入类似于以下内容时:
go run . "Hello\nThere" standard --output="banner.txt"
\n不会被识别为换行符,也不会起作用。
希望有人能帮助我解决这个问题!如果你有任何问题,请告诉我
英文:
As a project for my school, we need to do an ascii art text version. By this, i mean to use this command :
go run . "Hello World!" standard --output="banner.txt"
Where's standard is the figlet use for the ascii art, and output to put my result in a file call banner.txt.
For now, (and my code is very ugly) i got this :
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)
}
}
So, my biggest problem here is whenever i type something like this :
go run . "Hello\nThere" standard --output="banner.txt"
The \n don't count as a \n and do nothing.
I hope someone can help me for this ! And if you got any question just tell me
答案1
得分: 1
这有点棘手,因为shell不解释转义序列(正如Peter在他的评论中所澄清的那样)。
解决的方法是将参数传递为\\n
,例如:
go run . "Hello\\nThere" standard --output="banner.txt"
然后,在你的代码中,你可以使用strings
包中的Replace
函数来处理每个需要考虑换行符的参数,如下所示:
output := strings.Replace(os.Args[1], "\\n", "\n", -1)
这不是一个非常干净的解决方案,但它能完成任务。
英文:
This is a bit tricky, because the shell does not interpret escape sequences (as clarified by Peter in his comment).
A way to solve that is passing the parameter as \\n
so, for example:
go run . "Hello\\nThere" standard --output="banner.txt"
and then, in your code, you can use the Replace
function from the strings
package as follows on each parameter whose newline characters need to be considered:
output := strings.Replace(os.Args[1], "\\n", "\n", -1)
It is not a super clean solution but it does the job.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论