doushi8186 2018-08-08 11:11
浏览 172
已采纳

如何在Go中的os.Stdin和http上选择?

Say I want to accept an animal. The user can either set the type of animal at the What type of animal? prompt on a terminal, or she can go to http://localhost:1234/animal?type=kitten

Whichever she does, the terminal will read What type of animal? kitten (assuming she chose a kitten) and the program will then prompt the user on the terminal (and only the terminal) What is the kitten's name?

My thought was to use channels to go routines, but since both go routines will be stuck in a function call (Scan() for the terminal, ListenAndServe() for http) then I'm not clear how to stop the go routine that is still in a function call once the input is received. The normal method of selecting on channels won't work.

In C I'd just do a select(2) on the two relevant file descriptors (fd 0 and the fd from listen()) but I don't think that's how to do it in Go.

Note: I answered this down below. If you're down voting the question this I'd be curious to know why as I would have found the answer I came up with useful. If you're down voting the answer, I'd really be interested in knowing why. If it's non-idiomatic Go or if it has other issues I'm missing, I'd really like to fix them.

  • 写回答

3条回答 默认 最新

  • dongtang5057 2018-08-08 13:15
    关注

    OK, I figured out a solution for what I was trying to do.

    This might not be the most Go idiomatic way though. If the channels took a struct with input source and the string I could probably toss the select and the webinput channel.

    package main
    
    import (
        "bufio"
        "context"
        "fmt"
        "io"
        "net/http"
        "os"
        "time"
    )
    
    func getHTTPAnimal(input chan string) *http.Server {
        srv := &http.Server{Addr: ":1234"}
    
        http.HandleFunc("/animal", func(w http.ResponseWriter, r *http.Request) {
            animal, ok := r.URL.Query()["type"]
            if !ok || len(animal[0]) <= 0 {
                io.WriteString(w, "Animal not understood.
    ")
                return
            }
            io.WriteString(w, "Animal understood.
    ")
            input <- string(animal[0])
        })
    
        go func() {
            if err := srv.ListenAndServe(); err != nil {
                if err.Error() != "http: Server closed" {
                    fmt.Printf("getHTTPAnimal error with ListenAndServe: %s", err)
                }
            }
        }()
    
        return srv
    }
    
    func getTerminalInput(input chan string) {
        scanner := bufio.NewScanner(os.Stdin)
        for {
            scanner.Scan()
            input <- scanner.Text()
        }
    }
    
    func main() {
        input := make(chan string)
        webinput := make(chan string)
    
        srv := getHTTPAnimal(webinput)
        fmt.Print("Enter animal type: ")
        go getTerminalInput(input)
    
        var animal string
        select {
        case ta := <-input:
            animal = ta
        case webta := <-webinput:
            animal = webta
            fmt.Printf("%s
    ", animal)
        }
    
        ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
        srv.Shutdown(ctx)
        close(webinput)
    
        fmt.Printf("Enter animal name: ")
        name := <-input
        fmt.Printf("Congrats on getting %s the half-%s
    ", name, animal)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算