go - 作为一个单元测试用例,应该为这样的函数编写什么。代码片段会有所帮助

标签 go

func Home(w http.ResponseWriter, r *staticAuth.AuthenticatedRequest) {
    t, err := template.ParseFiles("index.html") //parse the html file homepage.html
    if err != nil {                             // if there is an error
        log.Print("template parsing error: ", err) // log it
    }
    err = t.Execute(w, nil) //execute the template and pass it the HomePageVars struct to fill in the gaps
    if err != nil {         // if there is an error
        log.Print("template executing error: ", err) //log it
    }
}

最佳答案

func Home(w http.ResponseWriter, r *staticAuth.AuthenticatedRequest) {
    t, err := template.ParseFiles("index.html") //parse the html file homepage.html
    if err != nil {                             // if there is an error
        log.Print("template parsing error: ", err) // log it
    }
    err = t.Execute(w, nil) //execute the template and pass it the HomePageVars struct to fill in the gaps
    if err != nil {         // if there is an error
        log.Print("template executing error: ", err) //log it
    }
}

在您的代码中,您检查并记录错误,然后继续并忽略错误。

关于go - 作为一个单元测试用例,应该为这样的函数编写什么。代码片段会有所帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48864836/

相关文章:

go - 如何将 int64 转换为二进制并在 golang 中保持前导零?

go - 我们如何处理特殊字符,如 "1¼",我想将其显示为 "1¼"

go - 在 Go 项目中组织 Assets 文件

json - Golang : json. Unmarshal 未正确返回数据

facebook-graph-api - Facebook服务器端登录、CORS

caching - 如何在 golang 中进行持久缓存?

go - Angular 5 和 Go 网络服务

regex - 不创建组如何表达 "match a word or another"?

Golang Couchbase 集成在设置/获取时抛出错误

Go generate 只扫描 main.go