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
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.
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
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
###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.
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
}
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}