How do I put float32 onto a web page for output?

blov · · 505 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Currently two programs exist, which only downloads the output instead of putting on a web page. My understanding in GO is nothing. This is all information found on the internet and nothing is helping. Making me /really/ hate this language despite its praise. PLEASE HELP. THANK YOU. PROGRAM1: main.go</p> <p>package main</p> <p>import ( &#34;encoding/binary&#34; &#34;log&#34; &#34;math&#34; &#34;net/http&#34; )</p> <p>func Float64bytes(float float32) []byte { bits := math.Float32bits(float) bytes := make([]byte, 8) binary.LittleEndian.PutUint32(bytes, bits) return bytes } func serve_files(w http.ResponseWriter, r <em>http.Request) { w.Write([]byte(Float64bytes (grading()))) /</em>if r.URL.Path == &#34;/&#34; { http.ServeFile(w, r, &#34;index.html&#34;) }*/</p> <p>} func serve_contact(w http.ResponseWriter, r *http.Request) { w.Write([]byte(&#34;Hello World! This is a contact page&#34;)) } func main() { http.HandleFunc(&#34;/&#34;, serve_files) http.HandleFunc(&#34;/contact&#34;, serve_contact) log.Fatal(http.ListenAndServe(&#34;:8080&#34;, nil))</p> <p>}</p> <p>PROGRAM 2: gradez.go package main</p> <p>import ( &#34;fmt&#34; //&#34;log&#34; //&#34;net/http&#34; //&#34;strings&#34; )</p> <p>func grading() (asdf float32) {</p> <pre><code>//variables needed for arrays var testGrades []float32 var nTests int var quizGrades []float32 var nQuizzes int var homeworkGrades []float32 var nHomeworks int //weights var testsWeight float32 var quizzesWeight float32 var homeworksWeight float32 fmt.Println(&#34;Please input the value of each assignment as a percentage.&#34;) fmt.Print(&#34;Tests: &#34;) fmt.Scanln(&amp;testsWeight) fmt.Print(&#34;Quiz: &#34;) fmt.Scanln(&amp;quizzesWeight) fmt.Print(&#34;Homework: &#34;) fmt.Scanln(&amp;homeworksWeight) fmt.Println(&#34;--------------------------------------------------------------&#34;) fmt.Println(&#34; &#34;) fmt.Println(&#34;How many tests have you had? &#34;) testOptions(&amp;testGrades, nTests) fmt.Println(&#34;--------------------------------------------------------------&#34;) fmt.Println(&#34; &#34;) fmt.Println(&#34;How many Quizes have you had? &#34;) quizOptions(&amp;quizGrades, nQuizzes) fmt.Println(&#34;--------------------------------------------------------------&#34;) fmt.Println(&#34; &#34;) fmt.Println(&#34;How many Homeworks have you had? &#34;) homeworkOptions(&amp;homeworkGrades, nHomeworks) asdf = calculations(testGrades, quizGrades, homeworkGrades, testsWeight, quizzesWeight, homeworksWeight) return </code></pre> <p>}</p> <p>/* loops through adding grades to testGrades Indicators and Pointers are a MUST</p> <p>*/</p> <p>func testOptions(testGrades *[]float32, nTests int) {</p> <pre><code>var counter int fmt.Scanln(&amp;nTests) for i := 0; i &lt; nTests; i++ { counter++ fmt.Print(&#34;What was your grade on Test &#34;) fmt.Print(counter) fmt.Println(&#34;?&#34;) var scoreInput float32 fmt.Scanln(&amp;scoreInput) *testGrades = append(*testGrades, scoreInput) } </code></pre> <p>}</p> <p>func quizOptions(quizGrades *[]float32, nQuizzes int) {</p> <pre><code>var counter int fmt.Scanln(&amp;nQuizzes) for i := 0; i &lt; nQuizzes; i++ { counter++ fmt.Print(&#34;What was your grade on Quiz &#34;) fmt.Print(counter) fmt.Println(&#34;?&#34;) var scoreInput float32 fmt.Scanln(&amp;scoreInput) *quizGrades = append(*quizGrades, scoreInput) } </code></pre> <p>}</p> <p>func homeworkOptions(homeworkGrades *[]float32, nHomeworks int) {</p> <pre><code>var counter int fmt.Scanln(&amp;nHomeworks) for i := 0; i &lt; nHomeworks; i++ { counter++ fmt.Print(&#34;What was your grade on Homework &#34;) fmt.Print(counter) fmt.Println(&#34;?&#34;) var scoreInput float32 fmt.Scanln(&amp;scoreInput) *homeworkGrades = append(*homeworkGrades, scoreInput) } </code></pre> <p>}</p> <p>/* Calculations */</p> <p>func calculations(testGrades, quizGrades, homeworkGrades []float32, testsWeight, quizzesWeight, homeworksWeight float32) (grade float32) {</p> <pre><code>/* for loops that reads through array maps _ is used to avoid a set decleration value, (float, int, String...) */ var tAverage float32 = 0 for _, value := range testGrades { tAverage += value } var qAverage float32 = 0 for _, value := range quizGrades { qAverage += value } var hAverage float32 = 0 for _, value := range homeworkGrades { hAverage += value } var tPercent float32 var qPercent float32 var hPercent float32 var sumPercent float32 // var grade float32 fmt.Print(&#34;Tests:&#34;) fmt.Println(testGrades) fmt.Print(&#34;Quizess:&#34;) fmt.Println(quizGrades) fmt.Print(&#34;Homeworks:&#34;) fmt.Println(homeworkGrades) fmt.Println(&#34;--------------------------------------------------------------&#34;) /* we also print like this */ /* fmt.Print(&#34;Tests Average:&#34;) fmt.Println(tAverage / float32(len(testGrades))) fmt.Print(&#34;Quizes average:&#34;) fmt.Println(qAverage / float32(len(testGrades))) fmt.Print(&#34;homework Average:&#34;) fmt.Println(hAverage / float32(len(homeworkGrades))) */ tAverage = tAverage / float32(len(testGrades)) fmt.Print(&#34;Your Test Average: &#34;) fmt.Println(tAverage) qAverage = qAverage / float32(len(quizGrades)) fmt.Print(&#34;Your Quiz Average: &#34;) fmt.Println(qAverage) hAverage = hAverage / float32(len(homeworkGrades)) fmt.Print(&#34;Your Test Average: &#34;) fmt.Println(hAverage) fmt.Println(&#34;--------------------------------------------------------------&#34;) tPercent = tAverage * (testsWeight / 100) qPercent = qAverage * (quizzesWeight / 100) hPercent = hAverage * (homeworksWeight / 100) sumPercent = tPercent + qPercent + hPercent grade = sumPercent / ((testsWeight / 100) + (quizzesWeight / 100) + (homeworksWeight / 100)) fmt.Print(&#34;Your Grade: &#34;) fmt.Println(grade) return </code></pre> <p>}</p> <hr/>**评论:**<br/><br/>justinisrael: <pre><blockquote> <p>My understanding in GO is nothing. This is all information found on the internet and nothing is helping. Making me /really/ hate this language despite its praise.</p> </blockquote> <p>I would suggest that you refrain from starting your question like this. Saying that you hate the language even though other people speak highly of it, because you know nothing about it and can&#39;t find answers on the internet, is not going to motivate people to read your wall of code and try to help. </p> <p>That being said, could you please state your goal a little more clearly? Are you expecting to be able to hit a certain http endpoint in your program and see the results of a function printed out? Do you need to have a web form to accept input? Do you need to accept any input?</p></pre>itsmontoya: <pre><p>You&#39;re trying to run before you can walk. Why not baby steps?</p></pre>ModerateBrainUsage: <pre><p>What&#39;s the goal of this program? There&#39;s probably a lot better way to do things than what you wrote now. I didn&#39;t check your program, since you don&#39;t say what your goals are, only that you hate it.</p> <p>To make a string out of a float use fmt.Sprintf(&#34;%2.5f&#34;, blah) or use fmt.Fprintf() to write to a io.Writer. Why float32 instead of float64? Are you using 32bit hardware? The math package works on float64.</p> <p>One last thing, you can replace a lot of fmt.Print() and fmt.Println() with fmt.Printf(). You can replace a lot of it with text/template too.</p> <p>As others have said, learn to walk first and then run. It doesn&#39;t look like you have even done the Go tour. This is on the 2nd page of it: <a href="https://tour.golang.org/basics/2" rel="nofollow">https://tour.golang.org/basics/2</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

505 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传