Created
April 19, 2018 07:57
-
-
Save OmisNomis/748d730ad3c63a0cd7e6fdb18dfdaefe to your computer and use it in GitHub Desktop.
Go RPC Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
) | |
// Make a new ToDo type that is a typed collection of fields | |
// (Title and Status), both of which are of type string | |
type ToDo struct { | |
Title, Status string | |
} | |
type EditToDo struct { | |
Title, NewTitle, NewStatus string | |
} | |
type Task int | |
// Declare variable 'todoSlice' that is a slice made up of | |
// type ToDo items | |
var todoSlice []ToDo | |
// GetToDo takes a string type and returns a ToDo | |
func (t *Task) GetToDo(title string, reply *ToDo) error { | |
var found ToDo | |
// Range statement that iterates over todoArray | |
// 'v' is the value of the current iterateee | |
for _, v := range todoSlice { | |
if v.Title == title { | |
found = v | |
} | |
} | |
// found will either be the found ToDo or a zerod ToDo | |
*reply = found | |
return nil | |
} | |
// MakeToDo takes a ToDo type and appends to the todoArray | |
func (t *Task) MakeToDo(todo ToDo, reply *ToDo) error { | |
todoSlice = append(todoSlice, todo) | |
*reply = todo | |
return nil | |
} | |
// EditToDo takes a string type and a ToDo type and edits an item in the todoArray | |
func (t *Task) EditToDo(todo EditToDo, reply *ToDo) error { | |
var edited ToDo | |
// 'i' is the index in the array and 'v' the value | |
for i, v := range todoSlice { | |
if v.Title == todo.Title { | |
todoSlice[i] = ToDo{todo.NewTitle, todo.NewStatus} | |
edited = ToDo{todo.NewTitle, todo.NewStatus} | |
} | |
} | |
// edited will be the edited ToDo or a zeroed ToDo | |
*reply = edited | |
return nil | |
} | |
// DeleteToDo takes a ToDo type and deletes it from todoArray | |
func (t *Task) DeleteToDo(todo ToDo, reply *ToDo) error { | |
var deleted ToDo | |
for i, v := range todoSlice { | |
if v.Title == todo.Title && v.Status == todo.Status { | |
// Delete ToDo by appending the items before it and those | |
// after to the todoArray variable | |
todoSlice = append(todoSlice[:i], todoSlice[i+1:]...) | |
deleted = todo | |
break | |
} | |
} | |
*reply = deleted | |
return nil | |
} | |
func main() { | |
task := new(Task) | |
var err error | |
var reply ToDo | |
log.Println("1. todo Slice: ", todoSlice) | |
finishApp := ToDo{"Finish App", "Started"} | |
makeDinner := ToDo{"Make Dinner", "Not Started"} | |
walkDog := ToDo{"Walk the dog", "Not Started"} | |
task.MakeToDo(finishApp, &reply) | |
task.MakeToDo(makeDinner, &reply) | |
task.MakeToDo(walkDog, &reply) | |
log.Println("2. todo Slice: ", todoSlice) | |
task.DeleteToDo(makeDinner, &reply) | |
log.Println("3. todo Slice: ", todoSlice) | |
task.MakeToDo(makeDinner, &reply) | |
log.Println("4. todo Slice: ", todoSlice) | |
task.GetToDo("Finish App", &reply) | |
log.Println("5.", reply) | |
task.GetToDo("Finish Application", &reply) | |
log.Println("6.", &reply) | |
err = task.EditToDo(EditToDo{"Finish App", "Finish App", "Completed"}, &reply) | |
if err != nil { | |
log.Fatal("Problem editing ToDo: ", err) | |
} | |
log.Println("7. todo Slice: ", todoSlice) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment