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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
1
2
3
4
5
6
7
2
3
4
5
6
7
# 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))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
1
2
3
4
5
6
7
2
3
4
5
6
7
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22