method_interf

package
v0.0.0-...-ca74b24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAndSet

func GetAndSet(x MyInterface, i int)

Now any function which takes MyInterface as a parameter will accept a variable of type *MyType.

Types

type MyInt

type MyInt int

func (MyInt) Get

func (p MyInt) Get() int

type MyInterface

type MyInterface interface {
	Get() int
	Set(i int)
}

type MySubType

type MySubType struct {
	MyType // an anonymous field of type MyType
	// contains filtered or unexported fields
}

This is not the same as a subclass in Java, but a form of delegation. When a method of an anonymous field is called, its receiver is the field, not the surrounding struct. In other words, methods on anonymous fields are not dynamically dispatched. When you want the equivalent of Java’s dynamic method lookup, use an interface.

func (*MySubType) Get

func (p *MySubType) Get() int

he Get method was overridden, and the Set method was inherited.

type MyType

type MyType struct {
	// contains filtered or unexported fields
}

func (*MyType) Get

func (p *MyType) Get() int

func (*MyType) Set

func (p *MyType) Set(i int)

Since MyType already has a Get method, we can make MyType satisfy the interface by adding

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL