比熊科技 | APP开发 | APP制作

2019-04-03 00:08:00

package main
import(
    "fmt"
    "net/http"
    "os"
    "bufio"
    "strings"
    "io"
)
func getHttpResponse(keyName string)(responseStr string){
    filePath := "/Users/bichonfrise/go/src/practise/practise.txt"
    //读取文件
    countNum := 0
    file, err := os.OpenFile(filePath,os.O_RDONLY,0666)
    if err != nil{
        fmt.Println(err)
        return "err"
    }
    defer file.Close()
    
    buf := bufio.NewReader(file)
    for {
        line,err := buf.ReadString('\n')
        if err != io.EOF {
            line = strings.TrimSpace(line)
            if strings.Index(line, keyName) != -1 {
                countNum = countNum+1;
            }
        }else{
            return fmt.Sprintf("Callback({errorcode:0,\"name\":\"%s\",\"num\":%d})",keyName,countNum)
        }   
    }

    return fmt.Sprintf("Callback({errorcode:0,\"name\":\"%s\",\"num\":%d})",keyName,countNum)
}
func handler(writer http.ResponseWriter, request *http.Request){
    //获取get参数
    querys := request.URL.Query()
    keyName,ok := querys["name"]
    if !ok {
        fmt.Fprintf(writer,"Need name param")
        return ;
    }
    //获取响应字符串
    responseStr := getHttpResponse(keyName[0])
    //获取
    fmt.Fprintf(writer,"%s",responseStr)
}
func main(){
    http.HandleFunc("/",handler);
    http.ListenAndServe(":8080",nil)
}

BichonfriseSOFT

西安比熊网络科技有限公司

如您有疑问或需要帮助,
可以咨询一下商务经理

电话咨询17792315676