亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

傳遞引用不更新字節數組

傳遞引用不更新字節數組

Go
慕斯王 2022-05-18 14:39:26
我正在嘗試為我正在處理的 Lambda 項目找出內存文件操作。這個虛擬程序說明了我的無知:package mainimport (    "bytes"    "fmt"    "log")func Append(input *[]byte, appendee string) (err error)  {   buf := bytes.NewBuffer(*input)   if _, err := buf.WriteString(appendee); err != nil {       log.Println(err)   }    return err}func main() {   test := "this is a test\nThis test is short\nBut longer tests would work also\n"   b := []byte(test)   fmt.Println(b)   err := Append(&b, "This is an appended line")   if err != nil {           fmt.Println("oops")   }   fmt.Println(b)}我希望 y 在調用 Append 的行中傳遞對字節數組的引用,該函數可以操作與 main 中的相同數據結構,然后打印將包括附加的行。但事實并非如此。這是執行:? go run test.go[116 104 105 115 32 105 115 32 97 32 116 101 115 116 10 84 104 105 115 32 116 101 115 116 32 105 115 32 115 104 111 114 116 10 66 117 116 32 108 111 110 103 101 114 32 116 101 115 116 115 32 119 111 117 108 100 32 119 111 114 107 32 97 108 115 111 10][116 104 105 115 32 105 115 32 97 32 116 101 115 116 10 84 104 105 115 32 116 101 115 116 32 105 115 32 115 104 111 114 116 10 66 117 116 32 108 111 110 103 101 114 32 116 101 115 116 115 32 119 111 117 108 100 32 119 111 114 107 32 97 108 115 111 10]我錯過了什么?
查看完整描述

2 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

*input = buf.Bytes()你之前return err在Append功能中失蹤了


func Append(input *[]byte, appendee string) (err error) {

    buf := bytes.NewBuffer(*input)

    if _, err := buf.WriteString(appendee); err != nil {

        log.Println(err)

    }

    *input = buf.Bytes()

    return err

}

看看 go playground 上的工作代碼。


查看完整回答
反對 回復 2022-05-18
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您缺少一行:


package main


import (

    "bytes"

    "fmt"

    "log"

)


// Using your function

func Append(input *[]byte, appendee string) error {

    buf := bytes.NewBuffer(*input)

    if _, err := buf.WriteString(appendee); err != nil {

        log.Println(err)

        return err

    }

    // You are missing this one

    *input = buf.Bytes()

    return nil

}


// Using another approach

func Append2(input *[]byte, a string) {

    sb := []byte(a)

    *input = append(*input, sb...)

}


// As pointed by @cerise-limón, is no necessary to perform type conversion from string to byte during the append

func Append3(input *[]byte, a string) {

    *input = append(*input, a...)

}


func main() {

    var (

        test     string = "this is a test\nThis test is short\nBut longer tests would work also\n"

        toAppend string = "This is an appended line\n"

    )

    b := []byte(test)

    fmt.Println(b)

    if err := Append(&b, "1) "+toAppend); err != nil {

        fmt.Printf("oops: %s\n", err.Error())

    }

    fmt.Println(string(b))


    Append2(&b, "2) "+toAppend)

    fmt.Println(string(b))

    Append3(&b, "3) "+toAppend)

    fmt.Println(string(b))

}


查看完整回答
反對 回復 2022-05-18
  • 2 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號