gpt4 book ai didi

go - 如何将字符串作为参数传递给 Golang 指针接收函数?

转载 作者:数据小太阳 更新时间:2023-10-29 03:43:22 25 4
gpt4 key购买 nike

<分区>

我正在尝试一种带有值和指针接收函数的简单 Go 结构。我无法将一个字符串作为参数传递给指针接收函数来修改结构数据。任何人都可以帮忙吗?

代码:

package main

import (
"fmt"
)

type book struct {
author string
name string
category string
price int16
}

func (b book) greet() string {
return "Welcome " + b.author
}

func (b *book) changeAuthor(author string) {
b.author = author
}

func main() {
book := book{author: "Arockia",
name: "Python shortcuts",
category: "IT",
price: 1500}
fmt.Println(book.author)
fmt.Println(book.greet())
fmt.Println(book.changeAuthor("arulnathan"))
fmt.Println(book.author)
}

错误:

.\struct_sample.go:29:31: book.changeAuthor(string("arulnathan")) used as value

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com