命令行参数`string.Contains`与硬编码参数的行为不同。

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

cmd line parameter string.Contains behaving differently from hardcoded parameter

问题

我想对这两个字符串的strings.Contains()调用为什么表现不同进行一些澄清。

package main

import (
	"strings"
	"os"
	"errors"
	"fmt"
)

func main() {

	hardcoded := "col1,col2,col3\nval1,val2,val3"
	if strings.Contains(hardcoded, "\n") == false {
		panic(errors.New("The hardcoded string should contain a new line"))
	}
	fmt.Println("New line found in hardcoded string")

	if len(os.Args) == 2 {
		fmt.Println("parameter:", os.Args[1])
		if strings.Contains(os.Args[1], "\n") == false {
			panic(errors.New("The parameter string should contain a new line."))
		}
		fmt.Println("New line found in parameter string")
	}

}

如果我使用以下命令运行:

go run input-tester.go col1,col2,col3\\nval1,val2,val3

我得到以下输出:

New line found in hardcoded string
parameter: col1,col2,col3\nval1,val2,val3
panic: The parameter string should contain a new line.

goroutine 1 [running]:
panic(0x497100, 0xc42000e310)
	/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
	/home/user/Desktop/input-tester.go:21 +0x343
exit status 2

我可以看到打印出的字符串与硬编码的字符串具有相同的格式,但是strings.Contains()却没有找到\n

我猜这是我的疏忽。有人能解释我漏掉或误解了什么吗?

英文:

I'm looking to get some clarification on why these two strings.Contains() calls behave differently.

package main

import (
	"strings"
	"os"
	"errors"
	"fmt"
)

func main() {

	hardcoded := "col1,col2,col3\nval1,val2,val3"
	if strings.Contains(hardcoded, "\n") == false {
		panic(errors.New("The hardcoded string should contain a new line"))
	}
	fmt.Println("New line found in hardcoded string")

	if len(os.Args) == 2 {
		fmt.Println("parameter:", os.Args[1])
		if strings.Contains(os.Args[1], "\n") == false {
			panic(errors.New("The parameter string should contain a new line."))
		}
		fmt.Println("New line found in parameter string")
	}

}

If I run this with

go run input-tester.go col1,col2,col3\\nval1,val2,val3

I get the following

New line found in hardcoded string
parameter: col1,col2,col3\nval1,val2,val3
panic: The parameter string should contain a new line.

goroutine 1 [running]:
panic(0x497100, 0xc42000e310)
	/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
	/home/user/Desktop/input-tester.go:21 +0x343
exit status 2

I can see that the string printed out is the same format as the string that is hardcoded yet the string.Contains() doesn't find the "\n".

I'm guessing this is an oversight on my part. Can anyone explain what I'm missing or misunderstanding?

答案1

得分: 1

它的行为不同,因为在硬编码中,\n 被视为换行参数。而在命令行参数中,参数类型是字符串,给定的条件是 \n,它被视为换行参数。简单来说,\n 与两个连续的字符 \n 进行比较,而不是与换行字符 \n 进行比较。

因此,在命令行参数中使用以下代码:

if strings.Contains(os.Args[1], `\n`) == false {
    panic(errors.New("The parameter string should contain a new line."))
}

参考:https://golang.org/ref/spec#String_literals

原始字符串字面值是反引号之间的字符序列,例如 foo。在引号内,除了反引号之外,任何字符都可以出现。原始字符串字面值的值是由引号之间的未解释字符(隐式 UTF-8 编码)组成的字符串;特别地,反斜杠没有特殊含义,字符串可以包含换行符。

英文:

It behaves differently because in hardcoded \n is considered as new line parameter.
And in command line arguments , argument type is string, where given condition is for "\n" which is considered as new line parameter.
Simply ` \n compaires with two consecutive characters "" and "n" not with "\n" a new line character.

So for command line arguments use,

if strings.Contains(os.Args[1], `\n`) == false {
        panic(errors.New("The parameter string should contain a new line."))
    }

Reference : https://golang.org/ref/spec#String_literals

>Raw string literals are character sequences between back quotes, as in foo. Within the quotes, any character may appear except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines.

huangapple
  • 本文由 发表于 2016年11月21日 20:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/40719846.html
匿名

发表评论

匿名网友

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

确定