<iframe style="border:1px solid" src="https://wide.b3log.org/playground/b0675af760ca48c4ea794e173f46731c.go?embed=true" width="99%" height="600"></iframe>
上面有两个方法一个是带星号的User, 一个是不带的星号的User,但我始终搞不清,星号在这种情况下应用有什么区别?
<a href="/user/dasenlin12" title="@dasenlin12">@dasenlin12</a> 明白了,<a href="/user/JY115" title="@JY115">@JY115</a> 彻明白了。
#3
更多评论
```
package main
import (
"fmt"
"strconv"
)
type User struct {
username string
password string
age int32
}
func (this User) Set() {
this.username = "set name"
}
func (this *User) Set2() {
this.username = "set name"
}
func main() {
user := &User{username: "a", password: "aaa", age: 25}
user.Set()
fmt.Println("user:", user)
user2 := &User{username: "a", password: "aaa", age: 25}
user2.Set2()
fmt.Println("user2:", user2)
}
```
运行结果
```
user: username:a password:aaa age:25
user2: username:set name password:aaa age:25
```
可见,带指针的struct函数可修改struct实例,而不带指针的函数只是对实例的拷贝进行操作
#2