Call an object method: Difference between revisions

(→‎{{header|Go}}: added class/instance distinction)
Line 30:
 
=={{header|Go}}==
Go distances itself from the word "object" and from many object oriented concepts. It does however have methods. Any user-defined type in Go can have methods and these work very much like "classinstance methods" of object oriented languages. The examples below illustrate details of Go methods and thus represent the concept of classinstance methods.
 
Go has no direct equivalent to class methods.
Go has no direct equivalent to instance methods. Instead, cases where you might first think to use instance methods can likely be handled with Go interfaces, functions as first class objects, functions as struct members, or functions as closures.
<lang go>type Foo int // some custom type
 
// method on the type itself; can be called on that type or its pointer
func (self Foo) ValueMethod(x int) { }
 
// method on the pointer to the type; can be called on pointers
func (self *Foo) PointerMethod(x int) { }
 
 
Anonymous user