4 ms·
You are right, the writing is wrong. I was trying to imply in the C++ class sense where we write functions directly inside a class, in Go, we have to define a f
by thewhitetulip 10y ago
You are right, the writing is wrong. I was trying to imply in the C++ class sense where we write functions directly inside a class, in Go, we have to define a function over a struct.
In your example:
you call the method using an instance of T right? that was what I was saying, we have to define methods outside the struct declaration and then call it using an object of struct.
(*t).F(nil)
- jamra 10y agoIf t is nil, the function is still called with a nil value for t.
- thewhitetulip 10y agocan we call the method without doing t.F()? like just F()? I accept I have to modify the statement I made.
- chrisbroadfoot 10y agoNo. F refers to a different function than t.F.
- thewhitetulip 10y agoYes that was my point when i said we can't declare functions inside struct We can have methods which work on objects on structs but never a function like class's method
- deleted 10y ago[deleted]