“go vet” command will give you unreachable code
I want to process to the end, but I get unreachable code.
How to return in for …
//main.go
package main
import "fmt"
func main() {
var inputs string
fmt.Println("Please enter the characters,Valid characters:【go】")
fmt.Scan(&inputs)
fmt.Println(returnfunc(inputs))
}
func returnfunc(input string)(a string){
switch input {
case "go":
message := "golang will start!"+"Go is a tool for managing Go source code..."
return message
default:
messageerror := "ERROR: " + input + " is an invalid error Target method: Take a close look at the valid code,【 go 】 is valid"
return messageerror
}
for {
folang := input
fmt.Println("start for")
return folang
}
fmt.Println("Ended.")
return
}
return means “exit the current function and pass the given return value to the calling function”, so of course code that follows a return and is in the same branch, can never be reached.
Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find errors not caught by the compilers.
$ go version
go version devel +e82c9bd8 Wed Sep 16 08:49:14 2020 +0000 linux/amd64
$ go run reach.go
before
$ go vet reach.go
# command-line-arguments
./reach.go:10:2: unreachable code
$
./prog.go:10:2: unreachable code
Go vet exited.
before