ReadLine和验证

In one file that's in path /operations/validate:

package operations

import (
   "strings"
)

func CheckEnv(environment string) bool {
   env := strings.ToLower(environment)
   return env == "qa" || env == "dev" || env == "prd"
}

In the next file that calls the function:

package prompts

import (
    "bufio"
    "fmt"
    "os"
    val "thirsty/operations"
)

// AskEnv prompts for Environment
func AskEnv() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter environment (ex: dev, qa, prd): ")
    environment, _ := reader.ReadString('
')
    fmt.Println(environment)
    env := val.CheckEnv(environment)
    if !env {
        fmt.Printf("Invalid Argument: No environment found for %s", environment)
        return
    }

    os.Setenv("env", environment)
    fmt.Printf("Environment set to %s", environment)
}

During the prompt, I've tried qa or dev but I keep getting false as response. I am not sure why this is occurring and would appreciate any insight provided.

bufio.Reader.ReadString includes the delimiter in the returned string, so your CheckEnv function ends up doing (for example):

return "qa
" == "qa" || "qa
" == "dev" || "qa
" == "prd"

which is of course never going to return true.

You can solve this by using one of the strings.Trim*, like TrimSpace, to remove the trailing line ending character(s) and other unwanted whitespace, like this:

func CheckEnv(environment string) bool {
   env := strings.ToLower(strings.TrimSpace(environment))
   return env == "qa" || env == "dev" || env == "prd"
}