From 31a22881befc67815294df5a658f658c3503e6ea Mon Sep 17 00:00:00 2001 From: colawarrior Date: Sun, 20 Nov 2016 11:23:07 +0800 Subject: [PATCH] Update statistics.go (#287) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当在网页的文本框中输入中文逗号来分隔的数字和汉字或者数字和英文的组合时,错误信息打印不正确的问题 --- eBook/exercises/chapter_15/statistics.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/eBook/exercises/chapter_15/statistics.go b/eBook/exercises/chapter_15/statistics.go index a280923..e49189e 100755 --- a/eBook/exercises/chapter_15/statistics.go +++ b/eBook/exercises/chapter_15/statistics.go @@ -53,8 +53,14 @@ func homePage(writer http.ResponseWriter, request *http.Request) { func processRequest(request *http.Request) ([]float64, string, bool) { var numbers []float64 + var text string if slice, found := request.Form["numbers"]; found && len(slice) > 0 { - text := strings.Replace(slice[0], ",", " ", -1) + //处理如果网页中输入的是中文逗号 + if strings.Contains(slice[0], ",") { + text = strings.Replace(slice[0], ",", " ", -1) + } else { + text = strings.Replace(slice[0], ",", " ", -1) + } for _, field := range strings.Fields(text) { if x, err := strconv.ParseFloat(field, 64); err != nil { return numbers, "'" + field + "' is invalid", false