This is a creation in Article, where the information may have evolved or changed. //Copyright leakage 2010-12 Qtrac Ltd.
//
This program or package and any associated files is licensed under the
Apache License, Version 2.0 (the "License"); These files
Except in compliance with the License. You can get a copy of the License
at:http://www.apache.org/licenses/license-2.0.
//
Unless required by applicable law or agreed to writing, software
Distributed under the License is distributed on a "as is" BASIS,
Without warranties or CONDITIONS of any KIND, either express or implied.
See the License for the specific language governing permissions and
Limitations under the License.
Package Main
Import (
"FMT"
"Log"
"Net/http"
"Sort"
"StrConv"
"Strings"
)
Const (
The constants defined below are actually defined in terms of an HTML
PageTop = ' <! DOCTYPE html>
<style>.error{color: #FF0000;} </style>
<body>
<p>computes Basic statistics for a given list of numbers</p> '
form = ' <form action= '/' method= ' POST >
<label for= "Numbers" >numbers (comma or space-separated): </label><br/>
<input type= "text" name= "Numbers" size= "><br/>"
<input type= "Submit" value= "Calculate" >
</form> '
Pagebottom = ' </body>
Refers to the style of the error definition
Anerror = ' <p class= ' error ' >%s</p> '
)
Type statistics struct {
Numbers []float64
Mean float64
Median float64
}
Func Main () {
http. Handlefunc ("/", homepage)
If err: = http. Listenandserve (": 9001", nil); Err! = Nil {
Log. Fatal ("Failed to start server", err)
}
}
Func Homepage (writer http. Responsewriter, request *http. Request) {
ERR: = Request. Parseform () //must be called before writing response
Fmt. Fprint (writer, pagetop, form)//costume
If there is an error, you will be printing a wrong message
If err! = Nil {
Fmt. fprintf (writer, anerror, err)
} else {
If numbers, message, OK: = ProcessRequest (Request); OK {
Stats: = getstats (numbers) //Get sorted and calculated data
Fmt. Fprint (writer, formatstats (stats))//Formatted data output
} else if message! = "" {
Fmt. fprintf (writer, anerror, message)
}
}
Fmt. Fprint (writer, pagebottom)//End of line
}
Func ProcessRequest (Request *http. Request) ([]float64, String, bool) {
var numbers []float64 //defined as slices
If slice, found: = Request. form["Numbers"]; Found && len (slice) > 0 {//Get Number Entry
Text: = Strings. Replace (Slice[0], ",", "",-1)//slice[0]?
For _, Field: = Range strings. Fields (text) {
If x, err: = StrConv. parsefloat (field, 64); Err! = Nil {//convert to floating point number
Return numbers, "'" + Field + "is invalid", false
} else {
Numbers = append (numbers, x)//Append to slice with built-in function append
}
}
}
If Len (numbers) = = 0 {
Return numbers, "", false//No data first time form is shown
}
Return numbers, "", True
}
Func formatstats (Stats statistics) string {
Formatted output
Return to FMT. Sprintf (' <table border= ' 1 ">
<tr><th colspan= "2" >Results</th></tr>
<tr><td>Numbers</td><td>%v</td></tr>
<tr><td>Count</td><td>%d</td></tr>
<tr><td>Mean</td><td>%f</td></tr>
<tr><td>Median</td><td>%f</td></tr>
</table> ', Stats.numbers, Len (stats.numbers), Stats.mean, Stats.median)
}
Func getstats (Numbers []float64) (stats statistics) {
Stats.numbers = Numbers //return value tips in the Go language
Sort. float64s (stats.numbers) //Sort Package
Stats.mean = SUM (numbers)/float64 (len (numbers))//average is converted to float64 first, otherwise there can be integers divided, rounded
Stats.median = median (numbers)
Return stats
}
Func sum (Numbers []float64) (total float64) {
For _, x: = range Numbers {
Total + = X
}
Return Total
}
Func median (numbers []float64) float64 {
Middle: = Len (numbers)/2
Result: = Numbers[middle]
If Len (numbers)%2 = = 0 {
result = (result + numbers[middle-1])/2
}
return result
}