如果我遇到 bufio.Scanner:token too long 错误,是否有任何方法可以用更大的缓冲区大小重试扫描?或者 bufio.Scanner 对我来说不是正确的选择,因为我不知道每个记录的确切大小?
谢谢
https://go.dev/play/p/Erx15nXXCGk
package main
import (
"bufio"
"bytes"
"fmt"
"strings"
)
func main() {
// Example byte array
byteArray := []byte("data1|data2|data3|data4|data5|data6|data7|data8|data9|data10")
// Buffer size for reading chunks
bufferSize := 6
buf := make([]byte, bufferSize)
// Create a bufio.Scanner with a custom split function
scanner := bufio.NewScanner(bytes.NewReader(byteArray))
scanner.Buffer(buf, bufferSize)
scanner.Split(splitFunc)
// Read the byte array in chunks
for scanner.Scan() {
// Process each token (chunk)
chunk := scanner.Text()
fmt.Println("Chunk:", chunk, "Chunk Length:", len(chunk))
}
if err := scanner.Err(); err != nil {
if err == bufio.ErrTooLong {
fmt.Println("Error:", err)
fmt.Printf("Buffer size %d is too small...\n", bufferSize)
} else {
fmt.Println("Error:", err)
}
}
}
func splitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Return nothing if at end of file and no data passed
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := strings.Index(string(data), "|"); i >= 0 {
return i + 1, data[0:i], nil
}
// If at end of file with data return the data
if atEOF {
return len(data), data, nil
}
return
}
nj = -10:10 % array of integers from -10 to 10
for m=1:length(nj)
if nj(m) == 0
B(m) = % (function)
else
B(m) = % (another function, dependent on m)
end
end
open class Base(
open val a: String,
open val b: String,
open val c: String,
) {
val concatenation = a + b + c
}
class Derived(
override val a: String,
override val b: String,
override val c: String
) : Base(a = a, b = b, c = c)
fun main() {
val derived = Derived(a = "foo", b = "bar", c = "baz")
println(derived.concatenation)
}
此示例打印出 "nullnullnull" ,而不是 "foobarbaz" .
但是如果你在超类中替换 val concatenation = a + b + c 它, fun concatenation() = a + b + c 它似乎可以很好地与该方法调用一起工作。
此外, Accessing non-final property <property> in constructor 使用时 val concatenation = a + b + c ,但我不确定这到底意味着什么。
初始化 Base 类和 Derived 类属性的顺序是否有什么问题?我认为可能是我 concatenation 从 Base 类中使用,但是对于继承,我也调用 Base 类构造函数,并且必须初始化具有相同字符串的相同属性。
class StrSinker(Sinker[str]):
def batch_write(self, data: list[int]) -> None:
pass
Pyright 将提出以下投诉:
Method "batch_write" overrides class "Sinker" in an incompatible manner
Parameter 2 type mismatch: base parameter is type "list[str]", override parameter is type "list[int]"
"list[str]" is incompatible with "list[int]"
Type parameter "_T@list" is invariant, but "str" is not the same as "int"
Consider switching from "list" to "Sequence" which is covariant (reportIncompatibleMethodOverride)
classdef MySolver
properties
parameter
Bform double {mustBeMember(Bform,[1,2])} = 1
B
end
methods
function B = get.B(obj)
% Calculates B according to the specific form
switch obj.Bform
case 1
B = obj.parameter(1)*obj.parameter(2);
case 2
B = 3*obj.parameter(1)*obj.parameter(2);
end
end
end
end
open class Base(
val a: String,
val b: String,
val c: String,
) {
val concatenation = a + b + c
}
class Derived(
a: String,
b: String,
c: String
) : Base(a = a, b = b, c = c)
fun main() {
val derived = Derived(a = "foo", b = "bar", c = "baz")
println(derived.concatenation)
}
分配属性初始值的代码被视为构造函数的一部分,因此您的代码 = a + b + c 是构造函数的一部分,并且是触发警告的原因。
以下是获取空值的过程。Derived 类构造函数将这三个值传递给超类构造函数。超类将这些值分配给超类的 a , b 、 和 c 属性的支持字段。然后 concatenation 调用 a , b 、 和 c 属性来获取它们的值,但由于我们实际上是 Derived 的一个实例,这些属性已被覆盖,并且指向与作为超类的一部分初始化的支持字段不同的支持字段。