-1

I have two questions about Go code. This is my program. This program controls the presence of a nickname in a "database".

package main

import "fmt"

func rcv(){
  if r := recover() ; r != nil {
    fmt.Println("retry.")
    main()  
  }
}
func main(){
defer rcv()
INSERT:
  fmt.Println("Insert a nickname: ")
  var s string
  fmt.Scanln(&s)
  switch { 
  case len(s) <= 0 : 
    fmt.Println(fmt.Errorf("error empty"))
    goto INSERT
  case s=="mark"||s=="andrea": 
    fmt.Println("Accept, the nickname is",s)
  default :
    fmt.Println("ATTENTION, nickname not found")
    panic("Error.")
  }
}

My questions are:

  1. Using the recover function ( rcv () ), at the end of it, the recalled of main() is sent running like another thread? The principal main() function ends when the second is executed or whenever the panic is raised, the recover function create another process? (example: if the input nickname is wrong 5 times, 5 more main functions are started, so 6 Main running functions?) If create multiple executions of the main(), is there a way to print threads/process id or something like that?

  2. I used the INSERT label and the goto construct. Can I use goto outside the main function (example: at the end of the rcv() function to return to the top of the main function)? I could use goto instead of calling the main() function at the end of the rcv()

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mx88
  • 27
  • 1
  • 1
  • 7
  • Have you tried? From the spec "The scope of a label is the body of the function in which it is declared and excludes the body of any nested function." – JimB Jul 06 '17 at 16:01
  • I have already tried inserting goto INSERT in the rcv () function but say me the label is not declared. – mx88 Jul 06 '17 at 16:06
  • Yes, so doesn't that answer the question? I don't understand question 1, you're not creating any new processes or threads, or even goroutines. Though I would recommend constructing this form another function, as simply calling `main()` is uncommon enough to cause confusion as to why you would do that at all. – JimB Jul 06 '17 at 16:09
  • Yes, I do not create process, thread or goroutines. But would i know if that main() call in rcv() function is good programming or is it not recommended? Can I replace it with a goto-like construct to go back to the beginning of the main () (label INSERT)? – mx88 Jul 06 '17 at 16:16

1 Answers1

0

1. About defer

defer statement execute function BEFORE exiting scope. So stack of caller-func is remaining while executing defer-func.

func foo() (err error) {
    defer func() {
        err = io.EOF
    }()
}

It will crash with stackoverflow

2. About goto

See documentation

A "goto" statement transfers control to the statement with the corresponding label within the same function.

So you can't label outside func.

3. About question

How do I get back to the beginning of the main () function?

package main

func Main() {
    // do your main
}

func main() {
    for {
        Main()
    }
}
mattn
  • 7,571
  • 30
  • 54
  • As I thought, the defer function is part of the main () function, so an additional call of the main () function does not end the previous one. This program does what I expect, but I have these doubts. How do I get back to the beginning of the main () function? Should I force it to stop or create other functions and manage it better? – mx88 Jul 06 '17 at 16:24
  • Just make Main for doing your main part like above. And make it loop. – mattn Jul 06 '17 at 16:27