Mahdi
Mahdi

Reputation: 2305

Do we have something same as Java and C# Object in Golang?

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

Upvotes: 4

Views: 3475

Answers (1)

user6169399
user6169399

Reputation:

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.

see: http://golang.org/doc/go_spec.html

Upvotes: 8

Related Questions