首页 > 其他分享 >【学到一个小技巧】context.WithValue 中的 key 可以是个空对象

【学到一个小技巧】context.WithValue 中的 key 可以是个空对象

时间:2023-09-29 11:55:05浏览次数:31  
标签:key netpoll WithValue ctx context ctxkey conn

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


从 netpoll 库看到这段代码:

type connkey struct{}

var ctxkey connkey

func prepare(conn netpoll.Connection) context.Context {
	mc := newSvrMuxConn(conn)
	ctx := context.WithValue(context.Background(), ctxkey, mc)  // 这个技巧很酷. 以前我都只知道写字符串
	return ctx
}

func handle(ctx context.Context, conn netpoll.Connection) (err error) {
	mc := ctx.Value(ctxkey).(*svrMuxConn)
    //....
}

标签:key,netpoll,WithValue,ctx,context,ctxkey,conn
From: https://www.cnblogs.com/ahfuzhang/p/17736882.html

相关文章

  • ChatGPT多个APIkey如何自主切换
    chatgpt目前账户大多数为18美金,那么用户在调用api时候出现金额不足要手动更换chatgpt当前方法为了解决多账户自主切换api使用。创建一个名为$arr的数组,它包含几个key。我们使用foreach循环,每次将数组中的一个字符串作为密钥请求cURL。如果cURL的返回值不是null,我们将退出循环。......
  • KEYENCE Programming Contest 2019
    A-Beginning排序以后判断一下是否为\(1,4,7,9\)即可。#include<iostream>#include<cstdio>#include<algorithm>usingnamespacestd;constintN=10;inta[N];intmain(){ for(inti=1;i<=4;i++) scanf("%d",&a[i]); sort(a+1,a+4+1......
  • Keyence Programming Contest 2020
    A-Painting每次取\(H,W\)中较大者涂就是了,输出\(\lceil\frac{n}{\max(H,W)}\rceil\)。代码:#include<iostream>#include<cstdio>usingnamespacestd;inth,w,n;intmain(){ scanf("%d%d%d",&h,&w,&n); if(h<w)swap(h,w); ......
  • 编程语言mojo报错:error: cannot call function that may raise in a context that can
    代码:frompythonimportPythonfnmain():#fnmain()raises:#ThisisequivalenttoPython's`importnumpyasnp`letnp=Python.import_module("numpy")leta=np.array([1,2,3])print(a) 运行报错:[02:19:48](mojo)dev......
  • redis存入key值乱码
    一问题我的key值是:codes:12345, 但存入redis后出现了乱码  二解决使用StringRedisTemplate 结果如下图 ......
  • golang Context应用举例
     Context本质golang标准库里Context实际上是一个接口(即一种编程规范、一种约定)。typeContextinterface{Deadline()(deadlinetime.Time,okbool)Done()<-chanstruct{}Err()errorValue(keyany)any} 通过查看源码里的注释,我们得到......
  • 转!!java ssh远程服务器报错 解决方法 Key exchange was not finished, connection is c
    转自:https://blog.csdn.net/weixin_45266856/article/details/129754122是ssh中的key交换算法不兼容导致 ssh报错解决:vi/etc/ssh/sshd_configKexAlgorithmsdiffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-gro......
  • Mac故障排查系列:redis删除key报错MISCONF Redis is configured to save RDB snapshots
    背景:Mac下使用AnotherRedisDesktopManager客户端,删除key,遇到报错:MISCONFRedisisconfiguredtosaveRDBsnapshots,butit'scurrentlyunabletopersisttodisk.Commandsthatmaymodifythedatasetaredisabled,becausethisinstanceisconfiguredtoreporte......
  • 错误 [email protected]: Permission denied (publickey). fatal: Could not read from r
    场景:本地git已登录了账号,需要在github下载项目时提示[email protected]:Permissiondenied(publickey).fatal:Couldnotreadfromremoterepository.,解决方法是获取本地的密钥,在gitHub中添加即可,不需要有账号上的对应,不需考虑本地切换git账号问题. 1.判断错误情况:permissio......
  • .net webapiSwagger启用ApiKey身份验证
     直接在SwaggerConfig中设置请求头,这里请求头用的默认值apiKey,也可以自己定义一个 privateconststringswaggerApikey="Authorization";GlobalConfiguration.Configuration.MessageHandlers.Add(newSwaggerMessageHandler());GlobalConfiguration.......