In Java and C#, the Object
class is implicitly parent of all classes that are defined. Is there something similar in go?

- 1,871
- 2
- 24
- 41
-
6Go types don't even have parents, let alone a common ancestor. – user2357112 Jul 27 '16 at 04:17
-
1There are no classes in go. What specific problem are you trying to solve? – Paul Hankin Jul 27 '16 at 05:05
-
In java there are some default implementation for methods like `getHashCode` or `toString`. What happens to them in golang? – Mahdi Jul 27 '16 at 06:21
-
1there is Stringer interface instead of toString: https://golang.org/doc/effective_go.html#interface_conversions – Jul 27 '16 at 06:23
-
1for better help please provide use cases in new questions with http://stackoverflow.com/help/mcve – Jul 27 '16 at 06:55
1 Answers
There is no inheritance in Go.
I think you are looking for interface
: Go: What's the meaning of interface{}?
but if you need something similar to Object (not Class) you may use interface
:
Variables of interface type also have a distinct dynamic type, which is the concrete type of the value assigned to the variable at run time (unless the value is the predeclared identifier nil, which has no type). The dynamic type may vary during execution but values stored in interface variables are always assignable to the static type of the variable.
var x interface{} // x is nil and has static type interface{} var v *T // v has value nil, static type *T x = 42 // x has value 42 and dynamic type int x = v // x has value (*T)(nil) and dynamic type *T
and:
Interface types:
An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface. The value of an uninitialized variable of interface type is nil.

- 1
- 1
-
3It's inaccurate to say that go isn't an OOP language. It has objects (of any user-defined type) and methods, just not inheritance. But inheritance is not a defining property of OO languages -- see https://en.wikipedia.org/wiki/Object-oriented_programming – Paul Hankin Jul 27 '16 at 05:12
-
2