2016-06-24 8 views
5

Esiste comunque la creazione di un valore nullo string in Go?Esiste comunque una stringa null terminata in Go?

Quello che sto attualmente cercando è a:="golang\0" ma sta mostrando errore di compilazione:

non-octal character in escape sequence: " 
+0

Se necessario, utilizzare '0' insted per completare il lavoro. – ameyCU

+0

Vedere: https://golang.org/ref/spec#String_literals. – Volker

+1

NUL è sfuggito come '\ x00' nelle stringhe. Inoltre, il linguaggio non fornisce stringhe terminate da NUL quindi ... sì, sei costretto a modificare ogni stringa. – toqueteos

risposta

14

Spec: String literals:

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.

Così \0 è una sequenza di illegale, è necessario utilizzare 3 cifre ottali:

s := "golang\000" 

Oppure utilizzare hex (Cifre 2 esadecimali) codice:

s := "golang\x00" 

oppure una sequenza unicode (4 cifre esadecimali):

s := "golang\u0000" 

Esempio:

s := "golang\000" 
fmt.Println([]byte(s)) 
s = "golang\x00" 
fmt.Println([]byte(s)) 
s = "golang\u0000" 
fmt.Println([]byte(s)) 

uscita: a finire con un byte 0-code (provalo su Go Playground).

[103 111 108 97 110 103 0] 
[103 111 108 97 110 103 0] 
[103 111 108 97 110 103 0] 
+0

Grazie a icza, mi ha davvero aiutato. –

Problemi correlati