登录
首页 >  Golang >  Go问答

Golang中如何使用Fprintf()实现覆写输出

来源:stackoverflow

时间:2024-02-14 17:15:15 255浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《Golang中如何使用Fprintf()实现覆写输出》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我的问题是:我想使用 golang 打印 html 代码,如下所示:

func main() {
    http.HandleFunc("/",procesarRaiz)
    http.ListenAndServe(":8000",nil)
}

func procesarRaiz2(res http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(res,"

Text one

") //then i want to remove text "text one" and put this: fmt.Fprintf(res,"

Text 2

") }

当我打开浏览器并连接到服务器时,我可以看到两个文本,但我想用文本 2 替换文本 1。我一直在寻找解决方案,但我找不到,如果有人可以帮助我,我将非常感激.


正确答案


你无法“替换”它,但也许你可以通过使用 if 语句来欺骗它:

func main() {
    http.HandleFunc("/", procesarRaiz)
    http.ListenAndServe(":8000", nil)
}

func procesarRaiz(res http.ResponseWriter, req *http.Request) {
    var str string
    condition := true

    if !condition {
        str = "

Text one

" } else { str = "

Text 2

" } // the text will be printed depending on which condition is fulfilled fmt.Fprintf(res, str) }

理论要掌握,实操不能落!以上关于《Golang中如何使用Fprintf()实现覆写输出》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>