V
V
veryoriginalnickname2021-08-13 15:30:14
go
veryoriginalnickname, 2021-08-13 15:30:14

Why does default work all the time?

When specifying the correct options, default still works. Why?

package _cmd

import (
  "fmt"
  "os"
)

func BootCmd() {
  if len(os.Args) > 1 {
    for index, element := range os.Args {
      if index == 1 && element == "-menu" {
        menuMaster()
        break
      }
      if index > 2 {
        return
      }
    }
  } else {
    return
  }
}

func menuMaster() {
  fmt.Println("--- Servus Menu ---")
  fmt.Println("0. Exit")
  fmt.Println("1. Continue booting")
  fmt.Println("2. Create superuser")
  var selected string
  scanner(selected)
  switch selected {
  case "0": os.Exit(1)
  case "1": return
  case "2": fmt.Println("окей")
  default:
    fmt.Println("Wrong selection. Try again.")
    menuMaster()
  }
  return
}


func scanner(writeTo string){
  _, err := fmt.Fscan(os.Stdin, &writeTo)
  if err != nil {
    panic(err)
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
12rbah, 2021-08-13
@veryoriginalnickname

You are transferring a copy of the string, so you always have an empty string when you select
Fix it like this

func scanner(writeTo *string){
  _, err := fmt.Fscan(os.Stdin, writeTo)
  if err != nil {
    panic(err)
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question