Bytes

Wikipedia: Byte
bytes is a first-class type distinct from strings — an immutable sequence of raw byte values. Use it for binary data, encoding, or low-level string work.

fmt := import("fmt")
b := bytes("hello")
fmt.println(len(b))
fmt.println(type_name(b))
fmt.println(b[0])
fmt.println(b[4])
buf := bytes(4)
fmt.println(len(buf))
fmt.println(buf[0])
fmt.println(string(b[0:3]))
fmt.println(string(b[3:]))
for i, v in bytes("ABC") {
    fmt.printf("  [%d] = %d\n", i, v)
}
fmt.println(string(b))
modified := string(b[0:1]) + "ELL" + string(b[4:])
fmt.println(string(bytes(modified)))
fmt.println(bytes("abc") == bytes("abc"))
fmt.println(bytes("abc") == bytes("xyz"))

try it

5
bytes
104
111
4
0
hel
lo
  [0] = 65
  [1] = 66
  [2] = 67
hello
hELLo
true
false
loading…