上下文
更新時間 2024-12-10 12:14:42
最近更新時間: 2024-12-10 12:14:42
分享文章
本文介紹在函數計算中Context的相關概念和使用示例。
上下文
當函數計算運行您的函數時,會將上下文(context.Context)對象傳遞到執行方法中。該對象包含有關調用、服務、函數和執行環境等信息。
示例
您可以 import gitee.com/ctyunfaas/cf-runtime-go-sdk/fccontext,通過fccontext.FromContext方法獲取fccontext。如下示例將輸出上下文相關信息:
package?main
import?(
???"context"
???"encoding/json"
???"gitee.com/ctyunfaas/cf-runtime-go-sdk/cf"
???"gitee.com/ctyunfaas/cf-runtime-go-sdk/fccontext"
)
func?main()?{
???cf.Start(HandleRequest)
}
func?HandleRequest(ctx?context.Context)?(string,?error)?{
???fctx,?_?:=?fccontext.FromContext(ctx)
???res,?_?:=?json.Marshal(fctx)
???return?string(res),?nil
}