04/02 Study Daily record

손진성·2022년 2월 4일
0

Methods

  • Go does not have classes.
  • A method is a function with special receiver argument.
    • func (recv ) fn()
    • The type to which the method belongs is known as the receiver.
  • has to be declared in the same package.
  • Pointer receivers are used when the method needs to change the values to which the receiver points.
  • Pointers only save memory because they only pass the memory addresses around
  • Simply put, a method is a special function that performs a specific function using the fields of a structure.
type SLLNode struct {
	next  *SLLNode
	value int
}

func (sNode *SLLNode) SetValue(v int) {
	sNode.value = v
}

func (sNode *SLLNode) GetValue() int {
	return sNode.value
}

func NewSLLNode() *SLLNode {
	return new(SLLNode)
}

func main() {
	node := NewSLLNode()
	node.SetValue(4)
	fmt.Println("Node is of value: ", node.GetValue())
}

Print Result

Node is of value:  4
  • go.dev/play/p/UNgnDTGPqDN
  • Because there were pointers, reference were passed.
type SLLNode struct {
	next  *SLLNode
	value int
}

func (sNode SLLNode) SetValue(v int) {
	sNode.value = v
}

func (sNode SLLNode) GetValue() int {
	return sNode.value
}

func NewSLLNode() SLLNode {
	return SLLNode{}
}

func main() {
	node := NewSLLNode()
	node.SetValue(4)
	fmt.Println("Node is of value: ", node.GetValue())
}

Print result

Node is of value:  0

-If pointers will not be used, values will be gone. because local values will stay in only when their function is processed.

  • go.dev/play/p/ot5QOqBEn-h
  • Pass by value is a method of copying and passing the value of an argument. Therefore, no matter what operation is performed on the copied value in the function, the original value does not change.
type level int

func main() {
	sl := new(level)
	sl.raiseShieldLevel(4)
	sl.raiseShieldLevel(5)

	fmt.Println(*sl)
}

func (lv *level) raiseShieldLevel(i int) {
	if *lv == 0 {
		*lv = 1
	}

	*lv = (*lv) * level(i)
}

Print Result

20

There are two ways to create a struct pointer.
1. Creating an object using'new(structname)'.
2. Prepending & in front of struct names

type level int

func main() {
	var sl level = 10
	sl.raiseShieldLevel(4) //(&sl).raiseShieldLevel(4)
	sl.raiseShieldLevel(5)

	fmt.Println(sl)
}

func (lv *level) raiseShieldLevel(i int) {
	if *lv == 0 {
		*lv = 1
	}

	*lv = (*lv) * level(i)
}

Print result

200
  • A method does not need to be attached to struct.
  • They can be attached to any type that we create and declare the condition.
  • var sl level = 10 // this is not a pointer, but Go will interpret and convert it.

Interfaces

  • A set of method signatures
  • Any type that implements interface methods is considered inplicitly to be a child of the interface
  • A value of interface type can hold any value that implements these methods
  • Pointers and functions implement interfaces too since they are all types
  • Go expects you to use built-in interfaces in order to expand functionaliyy, Exalple: Stringers
type Node interface {
	SetValue(v int)
	GetValue() int
}

//type SLLNode
type SLLNode struct {
	next  *SLLNode
	value int
}

func (sNode *SLLNode) SetValue(v int) {
	sNode.value = v
}

func (sNode *SLLNode) GetValue() int {
	return sNode.value
}

func NewSLLNode() *SLLNode {
	return new(SLLNode)
}

type PowerNode struct {
	next  *PowerNode
	value int
}

func (sNode *PowerNode) SetValue(v int) {
	sNode.value = v * 10
}

func (sNode *PowerNode) GetValue() int {
	return sNode.value
}

func NewPowerNode() *PowerNode {
	return new(PowerNode)
}

func main() {
	var node Node
	node = NewSLLNode()
	node.SetValue(4)
	fmt.Println("Node is of value: ", node.GetValue())

	node = NewPowerNode()
	node.SetValue(5)
	fmt.Println("Node is of value: ", node.GetValue())
}

Print result

Node is of value:  4
Node is of value:  50

-Because the type (structure) of the object to be passed is different, it is a method with the same function, but the parameters must be set differently. So, if you use an interface that binds the functions of the same property, you can call methods in the interface.
-Using an interface as a parameter means that you want to use the methods contained in the interface, regardless of the struct.

	if n, ok := node.(*PowerNode); ok {
		fmt.Println("This is a power node of value", n.GetValue())
	}

Print Result

	if n, ok := node.(*PowerNode); ok {
		fmt.Println("This is a power node of value", n.GetValue())
	}
Node is of value:  4
Node is of value:  50
This is a power node of value 50
  • go.dev/play/p/O5Krtq59lNB

###Single Linked Lists
-Each node stores a value
-Each node points to the next node

--A linked list whose nodes contain tow fields : an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list.

Interface - Stringers

  • A very common interface type in Go
  • The fmt package, with many others look at this interface when printing values
    type Stringer interface{
     String() string
     }
  
```go
func (list *SingleLinkedList) String() string {
	s := ""
	for n := list.head; n != nil; n = n.next {
		s += fmt.Sprintf("{%d} ", n.GetValue())
	}
	return s
}
  • One of the most widely used interfaces is Stringer from the fmt package.
  • The fmt package and many other packages use this interface to output values.
func main() {
	list := newSingleLinkedList()
	list.Add(3)
	list.Add(4)
	list.Add(5)
	list.Add(6)
	fmt.Println("Hello, playground", list)
}

Print Result

Hello, playground {3} {4} {5} {6} 

-Due to changed String, result was changed.
-Expected : Hello, playground &{0xc000046240 0xc000046270}

  • go.dev/play/p/zCpVd_7a19G
profile
Gopyther

0개의 댓글