简体   繁体   English

去获取目录名作为参数

[英]Go obtain a directory name as an argument

Go obtain a directory name from the сyrillic as an argument 从сyrillic获取目录名称作为参数

How to get the full path, not cut garbage. 如何获得完整的路径,而不是减少垃圾。 Example: The path as a program argument /home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/ 示例:作为程序参数的路径/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/

I get in the trimmed version of the program, how to fix? 我进入程序的精简版,该如何解决? /Home/spouk/spouk.download/torrent/Сергей

example simple code 示例简单代码

package main

import (
    "flag"
    "fmt"
)

func main() {
    wordPtr := flag.String("path", "foo", "a string")
    flag.Parse()
    fmt.Printf("Flag: -path=`%v`\nflag.Args: %v\n", *wordPtr, flag.Args())
    fmt.Printf("Rune: %v\n", []rune(*wordPtr))
}

This is not a matter of the Go code but how you pass it into your program, try passing the path in double quotes "…" 这与Go代码无关,而是如何将其传递到程序中,请尝试使用双引号"…"传递路径"…"

./main -path="/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/"
> Flag: -path=`/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/`
> flag.Args: []
> Rune: [47 104 111 109 101 47 115 112 111 117 107 47 115 112 111 117 107 46 100 111 119 110 108 111 97 100 47 116 111 114 114 101 110 116 47 1057 1077 1088 1075 1077 1081 32 1051 1091 1082 1100 1103 1085 1077 1085 1082 1086 44 32 1057 1086 1073 1088 1072 1085 1080 1077 32 1089 1086 1095 1080 1085 1077 1085 1080 1081 47]

This has nothing to do with Go. 这与Go无关。 This has nothing to do with cyrillic. 这与西里尔字母无关。 It is how the shell parses the command line. 这是shell解析命令行的方式。 It is using spaces to separate arguments. 它使用空格分隔参数。 Enclose the argument in double quotes (") to override. For example, 将参数括在双引号(“)中以进行覆盖。例如,

package main

import (
    "flag"
    "fmt"
    "os"
)

func main() {
    fmt.Printf("os.Args:   %q\n", os.Args[1:])
    wordPtr := flag.String("path", "foo", "a string")
    flag.Parse()
    fmt.Printf("flag.path: %q\n", *wordPtr)
    fmt.Printf("flag.Args: %q\n", flag.Args())
}

Output: 输出:

$ go run path.go -path=/home/spouk/Сергей Лукьяненко, Собрание сочинений/
os.Args:   ["-path=/home/spouk/Сергей" "Лукьяненко," "Собрание" "сочинений/"]
flag.path: "/home/spouk/Сергей"
flag.Args: ["Лукьяненко," "Собрание" "сочинений/"]

$ go run path.go -path="/home/spouk/Сергей Лукьяненко, Собрание сочинений/"
os.Args:   ["-path=/home/spouk/Сергей Лукьяненко, Собрание сочинений/"]
flag.path: "/home/spouk/Сергей Лукьяненко, Собрание сочинений/"
flag.Args: []

Bash Reference Manual Bash参考手册

3.1.2.3 Double Quotes 3.1.2.3双引号

Enclosing characters in double quotes ('"') preserves the literal value of all characters within the quotes [with some exceptions]. 用双引号('“')引起来的字符保留引号内所有字符的字面值[有些例外]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM