This commit is contained in:
Tnze
2022-01-22 21:39:40 +08:00
parent c724909cd7
commit e6c6684c25
2 changed files with 9 additions and 7 deletions

View File

@ -107,15 +107,15 @@ func (m Message) WriteTo(w io.Writer) (int64, error) {
func (m Message) Append(extraMsg ...Message) Message {
origLen := len(m.Extra)
finalLen := origLen + len(extraMsg)
var extra []Message
if cap(m.Extra) < len(m.Extra)+len(extraMsg) {
// pre expansion
extra := make([]Message, finalLen)
extra = make([]Message, finalLen)
copy(extra, m.Extra)
m.Extra = extra
}
for _, v := range extraMsg {
m.Extra = append(m.Extra, v)
} else {
extra = m.Extra[:finalLen]
}
copy(extra[origLen:], extraMsg)
m.Extra = extra
return m
}

View File

@ -123,8 +123,10 @@ func ExampleMessage_Append() {
Append(chat.Message{Text: "22222"}).
Append(chat.Message{Text: "333333"}).
Append(chat.Message{Text: "4444444"})
fmt.Print(msg)
fmt.Println(msg)
fmt.Println("debug: extra length:", len(msg.Extra))
// Output: 1111222223333334444444
// debug: extra length: 3
}
func ExampleTranslateMsg() {