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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀