go fiber http functions

huangapple go评论71阅读模式
英文:

go fiber http functions

问题

在"gofiber.io"的主页上,他们展示了以下代码:

package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    app.Get("/", func (c *fiber.Ctx) error {
        return c.SendString("Hello, World!")
    })

    log.Fatal(app.Listen(":3000"))
}

在这段代码中,Get函数的第二个参数是一个函数,它声明返回一个错误(error),但实际上返回的是c.SendString("Hello, World!")。我不明白为什么可以声明返回错误,但实际上返回的是其他类型的值。

我承认我对Golang和Go Fiber都很新,请帮助我更好地理解这个问题。

英文:

On the home page of "gofiber.io" they show this code

package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    app.Get("/", func (c *fiber.Ctx) error {
        return c.SendString("Hello, World!")
    })

    log.Fatal(app.Listen(":3000"))
}

In the code, the function inside the second argument of the "Get" function says it is returning an error but returns a c.SendString("Hello, World!"). What do I not understand about this that makes it ok to say you're returning an error but you are returning something else?

I admit I am new to Golang and even newer to Go Fiber so please help me understand this better.

答案1

得分: 0

SendString 方法返回的是发送请求后的错误信息(如果没有错误则返回 nil)。

详细信息请参考:https://docs.gofiber.io/api/ctx#send

该方法的签名如下:

func (c *Ctx) SendString(body string) error
英文:

The resulting error is returned from SendString (nil, if no error)

See https://docs.gofiber.io/api/ctx#send

The signature for is:

func (c *Ctx) SendString(body string) error

huangapple
  • 本文由 发表于 2022年12月31日 08:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74966906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定