用Golang写个99乘法表

99乘法表

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var result = make(map[int]string, 81)
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            result[i] += strconv.Itoa(j) + "乘以" + strconv.Itoa(i) + "等于:" + strconv.Itoa(i*j) + "  "
        }
        result[i] += "\n"
    }
    fmt.Println(result)
}

开启http服务web访问,隔行变色版

package main

import (
    "fmt"
    "net/http"
    "strconv"
)

func main() {
    http.HandleFunc("/", table99)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        fmt.Printf("http server failed, err:%v\n", err)
        return
    }
}
func table99(w http.ResponseWriter, r *http.Request) {
    str := "<table>"
    for i := 1; i < 10; i++ {
        if i%2 == 0 {
            str += "<tr style=\"background: red\">"
        } else {
            str += "<tr>"
        }
        for j := 1; j <= i; j++ {
            str += "<td>" + strconv.Itoa(j) + "乘以" + strconv.Itoa(i) + "等于:" + strconv.Itoa(i*j) + "</td>"
        }
        str += "</tr>"
    }
    str += "</table>"
    w.Write([]byte(str))
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注