Golang:试图弄清楚为什么”\n”不计数。

huangapple go评论68阅读模式
英文:

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不会被识别为换行符,也不会起作用。

希望有人能帮助我解决这个问题!如果你有任何问题,请告诉我 Golang:试图弄清楚为什么”\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 (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strings&quot;
)

func main() {

	if len(os.Args) == 4 {
		args := os.Args
		arg3 := os.Args[3]
		arg4 := strings.Split(arg3, &quot;=&quot;)

		if arg4[0] == &quot;--output&quot; {
			file, err := os.Create(arg4[1])

			if err != nil {
				fmt.Println(err)
				return
			}
			defer file.Close()

			police := &quot;&quot;
			if args != nil &amp;&amp; len(args) &gt; 1 {
				arg2 := os.Args[2]
				if arg2 == &quot;standard&quot; {
					police = &quot;standard.txt&quot;
				} else if arg2 == &quot;shadow&quot; {
					police = &quot;shadow.txt&quot;
				} else if arg2 == &quot;thinkertoy&quot; {
					police = &quot;thinkertoy.txt&quot;
				} else if arg2 == &quot;graffiti&quot; {
					police = &quot;graffiti.txt&quot;
				} else {
					fmt.Println(&quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&quot;)
					fmt.Println(&quot;	Figlet name invalid. Available : &quot;)
					fmt.Println(&quot;		- graffiti&quot;)
					fmt.Println(&quot;		- standard&quot;)
					fmt.Println(&quot;		- shadow&quot;)
					fmt.Println(&quot;		- thinkertoy&quot;)
				}
			}
			fichier, err := os.Open(police)

			if err != nil {
				fmt.Println(&quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&quot;)
				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&#39;instant)

			dec := 31 // correspond a la case zero de la map

			for _, ligne := range lignes { //ligne = une/chaques ligne dans l&#39;intervalle lignes du fichier txt
				if ligne == &quot;&quot; {
					dec++
				} else {
					asciiChrs[dec] = append(asciiChrs[dec], ligne)
				}
			}
			userInput := os.Args[1]
			invCharCount := 0

			for _, invalidChar := range userInput {

				if invalidChar &lt; 31 || invalidChar &gt; 126 {
					invCharCount++
					fmt.Println(&quot;Caract&#232;re non support&#233;: &quot;, string(invalidChar))

				}
			}

			if invCharCount &gt; 0 {
				fmt.Println(&quot;Try Again&quot;)
				os.Exit(0)
			} else if userInput == &quot;\\n&quot; {
				fmt.Print(&quot;\n&quot;)
				return
			} else if userInput == &quot;&quot; {
				return
			} else {
				Newline(userInput, asciiChrs)

			}
		} else {
			fmt.Println(&quot;Usage: go run . [STRING] [BANNER]\n\nEX: go run . something standard&quot;)

		}

	}

}

func Newline(n string, y map[int][]string) {
	replaceNewline := strings.Replace(n, &quot;\\n&quot;, &quot;\n&quot;, -1)

	wordsSlice := strings.Split(replaceNewline, &quot;\n&quot;)
	slice := []string{}
	slice2 := []string{}
	slice3 := []string{}
	slice4 := []string{}
	slice5 := []string{}
	slice6 := []string{}
	slice7 := []string{}
	slice8 := []string{}

	for _, mot := range wordsSlice {
		//for j := 0; j &lt; 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(&quot;banner.txt&quot;, os.O_APPEND|os.O_WRONLY, 0600)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	outpout := strings.Join(slice, &quot;&quot;)
	outpout2 := strings.Join(slice2, &quot;&quot;)
	outpout3 := strings.Join(slice3, &quot;&quot;)
	outpout4 := strings.Join(slice4, &quot;&quot;)
	outpout5 := strings.Join(slice5, &quot;&quot;)
	outpout6 := strings.Join(slice6, &quot;&quot;)
	outpout7 := strings.Join(slice7, &quot;&quot;)
	outpout8 := strings.Join(slice8, &quot;&quot;)

	if _, err = f.WriteString(outpout); err != nil {
		panic(err)
	}

	if _, err = f.WriteString(&quot;\n&quot;); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(outpout2); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(&quot;\n&quot;); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(outpout3); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(&quot;\n&quot;); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(outpout4); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(&quot;\n&quot;); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(outpout5); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(&quot;\n&quot;); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(outpout6); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(&quot;\n&quot;); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(outpout7); err != nil {
		panic(err)
	}
	if _, err = f.WriteString(&quot;\n&quot;); 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 Golang:试图弄清楚为什么”\n”不计数。

答案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 . &quot;Hello\\nThere&quot; standard --output=&quot;banner.txt&quot;

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], &quot;\\n&quot;, &quot;\n&quot;, -1)

It is not a super clean solution but it does the job.

huangapple
  • 本文由 发表于 2022年6月24日 22:01:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/72745243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定