{"id":207956,"date":"2025-07-08T12:17:38","date_gmt":"2025-07-08T04:17:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207956\/"},"modified":"2025-07-08T12:17:38","modified_gmt":"2025-07-08T04:17:38","slug":"golang%ef%bc%9a%e8%af%95%e5%9b%be%e6%89%be%e5%87%ba%e4%b8%ba%e4%bb%80%e4%b9%88n%e4%b8%8d%e9%87%8d%e8%a6%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207956\/","title":{"rendered":"Golang\uff1a\u8bd5\u56fe\u627e\u51fa\u4e3a\u4ec0\u4e48\u201c\\n\u201d\u4e0d\u91cd\u8981"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Golang\uff1a\u8bd5\u56fe\u627e\u51fa\u4e3a\u4ec0\u4e48\u201c\\n\u201d\u4e0d\u91cd\u8981<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 12:09:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang\uff1a\u8bd5\u56fe\u627e\u51fa\u4e3a\u4ec0\u4e48\u201c\\n\u201d\u4e0d\u91cd\u8981\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4f5c\u4e3a\u6211\u5b66\u6821\u7684\u4e00\u4e2a\u9879\u76ee\uff0c\u6211\u4eec\u9700\u8981\u5236\u4f5c\u4e00\u4e2a ascii \u827a\u672f\u6587\u672c\u7248\u672c\u3002\u6211\u7684\u610f\u601d\u662f\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4\uff1a<\/p>\n<p>\u5feb\u8dd1\u5427\u3002 \u201c\u4f60\u597d\u4e16\u754c\uff01\u201d\u6807\u51c6 &#8211;output=&#8221;banner.txt&#8221;<\/p>\n<p>\u5176\u4e2d\u7684\u6807\u51c6\u662f\u7528\u4e8e \u200b\u200bascii \u827a\u672f\u7684figlet\uff0c\u4ee5\u53ca\u5c06\u7ed3\u679c\u653e\u5165\u6587\u4ef6\u4e2d\u7684\u8f93\u51fa\uff0c\u8c03\u7528banner.txt\u3002<\/p>\n<p>\u73b0\u5728\uff0c\uff08\u6211\u7684\u4ee3\u7801\u975e\u5e38\u96be\u770b\uff09\u6211\u5f97\u5230\u4e86\u8fd9\u4e2a\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"bufio\"\n    \"fmt\"\n    \"os\"\n    \"strings\"\n)\n\nfunc main() {\n\n    if len(os.Args) == 4 {\n        args := os.Args\n        arg3 := os.Args[3]\n        arg4 := strings.Split(arg3, \"=\")\n\n        if arg4[0] == \"--output\" {\n            file, err := os.Create(arg4[1])\n\n            if err != nil {\n                fmt.Println(err)\n                return\n            }\n            defer file.Close()\n\n            police := \"\"\n            if args != nil &amp;&amp; len(args) &gt; 1 {\n                arg2 := os.Args[2]\n                if arg2 == \"standard\" {\n                    police = \"standard.txt\"\n                } else if arg2 == \"shadow\" {\n                    police = \"shadow.txt\"\n                } else if arg2 == \"thinkertoy\" {\n                    police = \"thinkertoy.txt\"\n                } else if arg2 == \"graffiti\" {\n                    police = \"graffiti.txt\"\n                } else {\n                    fmt.Println(\"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\")\n                    fmt.Println(\"   Figlet name invalid. Available : \")\n                    fmt.Println(\"       - graffiti\")\n                    fmt.Println(\"       - standard\")\n                    fmt.Println(\"       - shadow\")\n                    fmt.Println(\"       - thinkertoy\")\n                }\n            }\n            fichier, err := os.Open(police)\n\n            if err != nil {\n                fmt.Println(\"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\")\n                return\n            }\n\n            scanner := bufio.NewScanner(fichier) \/\/ Scan le fichier\n\n            scanner.Split(bufio.ScanLines) \/\/ split le scan en lignes\n\n            var lignes []string \/\/ englobe toutes les lignes\n            for scanner.Scan() {\n                lignes = append(lignes, scanner.Text())\n            }\n\n            asciiChrs := make(map[int][]string) \/\/ mise en place de la map (vide pour l'instant)\n\n            dec := 31 \/\/ correspond a la case zero de la map\n\n            for _, ligne := range lignes { \/\/ligne = une\/chaques ligne dans l'intervalle lignes du fichier txt\n                if ligne == \"\" {\n                    dec++\n                } else {\n                    asciiChrs[dec] = append(asciiChrs[dec], ligne)\n                }\n            }\n            userInput := os.Args[1]\n            invCharCount := 0\n\n            for _, invalidChar := range userInput {\n\n                if invalidChar &lt; 31 || invalidChar &gt; 126 {\n                    invCharCount++\n                    fmt.Println(\"Caract\u00e8re non support\u00e9: \", string(invalidChar))\n\n                }\n            }\n\n            if invCharCount &gt; 0 {\n                fmt.Println(\"Try Again\")\n                os.Exit(0)\n            } else if userInput == \"\\\\n\" {\n                fmt.Print(\"\\n\")\n                return\n            } else if userInput == \"\" {\n                return\n            } else {\n                Newline(userInput, asciiChrs)\n\n            }\n        } else {\n            fmt.Println(\"Usage: go run . [STRING] [BANNER]\\n\\nEX: go run . something standard\")\n\n        }\n\n    }\n\n}\n\nfunc Newline(n string, y map[int][]string) {\n    replaceNewline := strings.Replace(n, \"\\\\n\", \"\\n\", -1)\n\n    wordsSlice := strings.Split(replaceNewline, \"\\n\")\n    slice := []string{}\n    slice2 := []string{}\n    slice3 := []string{}\n    slice4 := []string{}\n    slice5 := []string{}\n    slice6 := []string{}\n    slice7 := []string{}\n    slice8 := []string{}\n\n    for _, mot := range wordsSlice {\n        \/\/for j := 0; j &lt; 8; \/*lignes*\/ j++ {\n        for _, lettre := range mot {\n            \/\/fmt.Print(y[int(lettre)][j])\n            slice = append(slice, (y[int(lettre)][0]))\n            slice2 = append(slice2, (y[int(lettre)][1]))\n            slice3 = append(slice3, (y[int(lettre)][2]))\n            slice4 = append(slice4, (y[int(lettre)][3]))\n            slice5 = append(slice5, (y[int(lettre)][4]))\n            slice6 = append(slice6, (y[int(lettre)][5]))\n            slice7 = append(slice7, (y[int(lettre)][6]))\n            slice8 = append(slice8, (y[int(lettre)][7]))\n        }\n        fmt.Println()\n        \/\/  }\n    }\n    f, err := os.OpenFile(\"banner.txt\", os.O_APPEND|os.O_WRONLY, 0600)\n    if err != nil {\n        panic(err)\n    }\n    defer f.Close()\n\n    outpout := strings.Join(slice, \"\")\n    outpout2 := strings.Join(slice2, \"\")\n    outpout3 := strings.Join(slice3, \"\")\n    outpout4 := strings.Join(slice4, \"\")\n    outpout5 := strings.Join(slice5, \"\")\n    outpout6 := strings.Join(slice6, \"\")\n    outpout7 := strings.Join(slice7, \"\")\n    outpout8 := strings.Join(slice8, \"\")\n\n    if _, err = f.WriteString(outpout); err != nil {\n        panic(err)\n    }\n\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout2); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout3); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout4); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout5); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout6); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout7); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(\"\\n\"); err != nil {\n        panic(err)\n    }\n    if _, err = f.WriteString(outpout8); err != nil {\n        panic(err)\n    }\n\n}\n<\/pre>\n<p>\u6240\u4ee5\uff0c\u6211\u6700\u5927\u7684\u95ee\u9898\u662f\u6bcf\u5f53\u6211\u8f93\u5165\u8fd9\u6837\u7684\u5185\u5bb9\u65f6\uff1a<\/p>\n<p>\u5feb\u8dd1\u5427\u3002 \u201c\u4f60\u597d\\n\u90a3\u91cc\u201d\u6807\u51c6 &#8211;output=&#8221;banner.txt&#8221;<\/p>\n<p>\\n \u4e0d\u7b97\u4f5c \\n \u5e76\u4e14\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/p>\n<p>\u6211\u5e0c\u671b\u6709\u4eba\u80fd\u5e2e\u52a9\u6211\uff01\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u8bf7\u544a\u8bc9\u6211:)<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u6709\u70b9\u68d8\u624b\uff0c\u56e0\u4e3a shell \u4e0d\u89e3\u91ca\u8f6c\u4e49\u5e8f\u5217\uff08\u6b63\u5982 peter \u5728\u4ed6\u7684\u8bc4\u8bba\u4e2d\u6240\u6f84\u6e05\u7684\u90a3\u6837\uff09\u3002<\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u5c06\u53c2\u6570\u4f5c\u4e3a <code>\\\\n<\/code> \u4f20\u9012\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>go run . \"hello\\\\nthere\" standard --output=\"banner.txt\"\n<\/pre>\n<p>\u7136\u540e\uff0c\u5728\u60a8\u7684\u4ee3\u7801\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5bf9\u9700\u8981\u8003\u8651\u6362\u884c\u7b26\u7684\u6bcf\u4e2a\u53c2\u6570\u4f7f\u7528 <code>strings<\/code> \u5305\u4e2d\u7684 <code>replace<\/code> \u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>output := strings.Replace(os.Args[1], \"\\\\n\", \"\\n\", -1)\n<\/pre>\n<p>\u8fd9\u4e0d\u662f\u4e00\u4e2a\u8d85\u7ea7\u5e72\u51c0\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u5b83\u53ef\u4ee5\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aGolang\uff1a\u8bd5\u56fe\u627e\u51fa\u4e3a\u4ec0\u4e48\u201c\\n\u201d\u4e0d\u91cd\u8981\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207956","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207956"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207956\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}