In this Getting Started with Golang, we will learn how to use the Golang Chaining Functions that serves as a sequence or series of related function events in Golang with step by step guide in Golang programming language.
#MaharlikansCode
#GolangChainingFunctions
#Golang
#LifeAsSoftwareDeveloper
#Maharlikans
#FilipinoSoftwareDeveloper
If you go with extra mile for buying me a cup of coffee, I appreciate it guys: https://ko-fi.com/maharlikanscode
Source Codes:
package main
import (
"fmt"
"strings"
)
// Tasks holds the data structure for the current task name
type Tasks struct {
Name string
EventType int
}
// TK initializes the 'Tasks' struct with an empty values
var TK = Tasks{}
// TaskName is the first chain layer
func TaskName(taskName string) *Tasks {
if len(strings.TrimSpace(taskName)) == 0 {
return &Tasks{}
}
newTask := Tasks{
Name: taskName,
}
fmt.Println("1st chain")
return &newTask
}
// Event is the 2nd chain layer
func (t *Tasks) Event(eventType int) *Tasks {
t.EventType = eventType
fmt.Println("2nd chain")
return t
}
// AddTask is the 3rd chain layer
func (t *Tasks) AddTask() *Tasks {
fmt.Println("3rd chain")
fmt.Println("task name: ", t.Name, " event type: ", t.EventType)
return t
}
func main() {
n := TaskName("Things to Do").Event(7).AddTask()
fmt.Println("n:", n.Name)
}