uint8 arrays

uint8 arrays


Node.js

const array = new Uint8Array(10)
console.log(array)

const offset = 1

array.set([1, 2, 3], offset)
console.log(array)

const sub = array.subarray(2)
console.log(sub)

const sub2 = array.subarray(2,4)
console.log(sub2)

console.log(array)
const value = 9
const start = 5
const end = 10
array.fill(value, start, end)
console.log(array)

console.log(array.byteLength)

Output

Uint8Array [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
Uint8Array [ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0 ]
Uint8Array [ 2, 3, 0, 0, 0, 0, 0, 0 ]
Uint8Array [ 2, 3 ]
Uint8Array [ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0 ]
Uint8Array [ 0, 1, 2, 3, 0, 9, 9, 9, 9, 9 ]
10

Go

package main

import "fmt"

func main() {
    array := make([]uint8, 10)
    fmt.Println(array)

    offset := 1

    copy(array[offset:], []uint8{1, 2, 3})
    fmt.Println(array)

    sub := array[2:]
    fmt.Println(sub)

    sub2 := array[2:4]
    fmt.Println(sub2)

    fmt.Println(array)
    value := uint8(9)
    start := 5
    end := 10
    for i := start; i < end; i++ {
        array[i] = value
    }
    fmt.Println(array)

    fmt.Println(len(array))
}

Output

[0 0 0 0 0 0 0 0 0 0]
[0 1 2 3 0 0 0 0 0 0]
[2 3 0 0 0 0 0 0]
[2 3]
[0 1 2 3 0 0 0 0 0 0]
[0 1 2 3 0 9 9 9 9 9]
10