2 ms·
Go lacks the C++ reference operator. You can't write: func fn(m &map[int]int) { m = make(map[int]int) } ... fn(m) You have to write:
by Animats 10y ago
Go lacks the C++ reference operator.
You can't write:
func fn(m &map[int]int) {
m = make(map[int]int)
}
...
fn(m)
You have to write:
func fn(m *map[int]int) {
*m = make(map[int]int)
}
...
fn(&m)
It's very C. The caller is forced to use different syntax to pass something as a pointer/reference. In C++ and Rust, you can't see from the call whether you're passing a pointer or a reference.
- mastax 10y agoThe difference is in C++ rvalues coerce to const&, while in Rust they do not. E.x. this works int square(const int & num) { return num * num; } int main() { square(5); } this does not fn takes_ref(a: &i32) -> i32 { *a } fn main() { takes_ref(64); } There are still cases where you won't be able to tell if you're using a ref or not, though: let x = returns_a_ref(); takes_ref(x);