specifications

Do things small program, is to imitate the mobile phone “reminder function” with Go to do a few simple add, delete, change and check operations, including information persistent storage using JSON format storage, do things expired can not be modified and other features. Summed up into several functions:

  1. Displays the day’s to-do list
  2. Add, delete, modify and check to-do items
  3. Expired to-do items cannot be edited
  4. Data is stored persistently
  5. Program output is highlighted

Program source Github to do things small program source Github address

Master the skill

  • Add, delete, and check json
  • Pointer to the
  • The structure of the body
  • function
  • mapping
  • Time module

Program function effect

New features

Check the function

Delete function

Change the function

Exit function

Core source code analysis

Data format field

The number of data fields is small, and the Items field is mainly a list nested map

{" Items ": [{" Content" : "the 2021 Olympic Games," "Time" : "2021/07/28"},... }Copy the code

Struct structure is defined in go language to interconnect with JSON format

type ItemsList struct {
  Content string `json: "content"`
  Time    string `json: "time"`
}
​
type Todo struct {
  Items []ItemsList `json: "items"`
}
Copy the code

Two ways to read JSON data

Added agent function, using struct to receive JSON data

// Add to-do items
func addTodo(a) {
  var Item ItemsList
  // Add to the to-do list
  fmt.Printf("Contents of additional matters to be done:")
  fmt.Scanf("%s\n", &Item.Content)
  // Please enter the custom time and verify the time format
  fmt.Println("Time Format 2021/07/07")
  fmt.Printf("Enter the time to do business:")
  fmt.Scanf("%s\n", &Item.Time)
​
  // Read the JSON file
  data, err := ioutil.ReadFile(JSONFILE)
  checkErr(err)
  newTodo := &Todo{}
  err = json.Unmarshal(data, &newTodo)
  checkErr(err)
​
  // Create a slice to summarize the new to-do items
  item := []map[string]string{}
​
  // Struct Transfer map type
  for _, data := range newTodo.Items {
    itemMap := make(map[string]interface{})
    obj1 := reflect.TypeOf(data)
    obj2 := reflect.ValueOf(data)
    for i := 0; i < obj1.NumField(); i++ {
      itemMap[obj1.Field(i).Name] = obj2.Field(i).Interface()
    }
    // Combine into a single item to do
    subItemMap := map[string]string{}
    for k, v := range itemMap {
      // Interface type conversion is string
      ov, _ := v.(string)
      subItemMap[k] = ov
    }
    // Summarize the local JSON items to do
    item = append(item, subItemMap)
  }
​
  // Add matters to do and merge
  item = append(item, map[string]string{"Content": Item.Content, "Time": Item.Time})
​
  // Write a JSON file
  writeJson(&item)
  formatInput(">> New successfully")}Copy the code

Other functions: Receive JSON data in map mode

// Delete the to-do list
func deleteTodo(a) {
  var no int
  fmt.Printf("Enter the number of the item to be deleted:")
  fmt.Scanf("%d\n", &no)
​
  // Read the JSON file
  mapTodo := make(map[string]interface{})
  readJson(&mapTodo)
​
  // Add the to-do list
  item := []map[string]string{}
  for index, v := range mapTodo["Items"([]].interface{}) {
    v := v.(map[string]interface{})
    ifno ! = index {// Matters to do for you: merger
      item = append(item, map[string]string{"Content": v["Content"]. (string), "Time": v["Time"]. (string)}}}// Write a JSON file
  writeJson(&item)
  formatInput(">> Delete succeeded")}Copy the code

In summary, using these two approaches, you can better understand go’s conversion between various data formats.

More small program cases, continue to update…. Guys, stay tuned