Go에서 어쨌든 null string
이 종료 되었습니까?Go에 null로 끝나는 문자열을 만드는 방법이 있습니까?
내가 현재 노력하고있어 a:="golang\0"
이지만이 표시되어 컴파일 오류 :
non-octal character in escape sequence: "
Go에서 어쨌든 null string
이 종료 되었습니까?Go에 null로 끝나는 문자열을 만드는 방법이 있습니까?
내가 현재 노력하고있어 a:="golang\0"
이지만이 표시되어 컴파일 오류 :
non-octal character in escape sequence: "
The text between the quotes forms the value of the literal, with backslash escapes interpreted as they are in rune literals (except that
\'
is illegal and\"
is legal), with the same restrictions. The three-digit octal (\nnn
) and two-digit hexadecimal (\xnn
) escapes represent individual bytes of the resulting string; all other escapes represent the (possibly multi-byte) UTF-8 encoding of individual characters.
그래서 \0
가 불법 순서는, 당신은 3 8 진수를 사용해야합니다 :
s := "golang\000"
또는 16 진수 코드 (2 진수 숫자)
s := "golang\x00"
또는 유니 서열 (4 자리 16 진수)
s := "golang\u0000"
예 :
s := "golang\000"
fmt.Println([]byte(s))
s = "golang\x00"
fmt.Println([]byte(s))
s = "golang\u0000"
fmt.Println([]byte(s))
출력 : 0 코드 바이트 모든 단부 (Go Playground에서 시도해보십시오).
[103 111 108 97 110 103 0]
[103 111 108 97 110 103 0]
[103 111 108 97 110 103 0]
감사합니다 icza, 정말 도움이되었습니다. –
필요한 경우 '0' insted를 사용하여 작업을 완료하십시오. – ameyCU
참조 : https://golang.org/ref/spec#String_literals. – Volker
NUL은 문자열에서'\ x00 '로 이스케이프됩니다. 또한 언어는 NUL 종료 문자열을 제공하지 않으므로 모든 문자열을 수정해야합니다. – toqueteos