struct User { var: is_registered bool } fn (var u User) register() { u.is_registered = true } var user := User{} println(user.is_registered) // "false" user.register() println(user.is_registered) // "true"
在本例中,receiver(它只是第一个参数)被标记为可变的,因此register()可以更改user对象。同样适用于非接收方参数:
fn multiply_by_2(var arr []int) { for i in 0..arr.len { arr[i] *= 2 } } var nums := [1, 2, 3] multiply_by_2(var nums) println(nums) // "[2, 4, 6]"
fn register(u User) User { return { u | is_registered: true } } user = register(user)