首页 > 其他分享 >网络协议中的数据结构

网络协议中的数据结构

时间:2023-01-31 14:33:31浏览次数:39  
标签:Context 网络协议 when returns Done context 数据结构 closed

我对Context和Handler是最迷惑的,什么玩意儿,用他就实现通信啦??
没有直观理解,不知道到底扮演一个什么角色。

1、Context
上下文

可以从里面获取session等对象,是个容器。
准确说是个map,里面装满了key-value,调用Value(key)函数可以获取key对应的value。

以go的源码为例,import "context"包下的context.go-Context接口:

点击查看代码
// A Context carries a deadline, a cancellation signal, and other values across
// API boundaries.
//
// Context's methods may be called by multiple goroutines simultaneously.
type Context interface {
	// Deadline returns the time when work done on behalf of this context
	// should be canceled. Deadline returns ok==false when no deadline is
	// set. Successive calls to Deadline return the same results.
	Deadline() (deadline time.Time, ok bool)

	// Done returns a channel that's closed when work done on behalf of this
	// context should be canceled. Done may return nil if this context can
	// never be canceled. Successive calls to Done return the same value.
	// The close of the Done channel may happen asynchronously,
	// after the cancel function returns.
	//
	// WithCancel arranges for Done to be closed when cancel is called;
	// WithDeadline arranges for Done to be closed when the deadline
	// expires; WithTimeout arranges for Done to be closed when the timeout
	// elapses.
	//
	// Done is provided for use in select statements:
	//
	//  // Stream generates values with DoSomething and sends them to out
	//  // until DoSomething returns an error or ctx.Done is closed.
	//  func Stream(ctx context.Context, out chan<- Value) error {
	//  	for {
	//  		v, err := DoSomething(ctx)
	//  		if err != nil {
	//  			return err
	//  		}
	//  		select {
	//  		case <-ctx.Done():
	//  			return ctx.Err()
	//  		case out <- v:
	//  		}
	//  	}
	//  }
	//
	// See https://blog.golang.org/pipelines for more examples of how to use
	// a Done channel for cancellation.
	Done() <-chan struct{}

	// If Done is not yet closed, Err returns nil.
	// If Done is closed, Err returns a non-nil error explaining why:
	// Canceled if the context was canceled
	// or DeadlineExceeded if the context's deadline passed.
	// After Err returns a non-nil error, successive calls to Err return the same error.
	Err() error

	// Value returns the value associated with this context for key, or nil
	// if no value is associated with key. Successive calls to Value with
	// the same key returns the same result.
	//
	// Use context values only for request-scoped data that transits
	// processes and API boundaries, not for passing optional parameters to
	// functions.
	//
	// A key identifies a specific value in a Context. Functions that wish
	// to store values in Context typically allocate a key in a global
	// variable then use that key as the argument to context.WithValue and
	// Context.Value. A key can be any type that supports equality;
	// packages should define keys as an unexported type to avoid
	// collisions.
	//
	// Packages that define a Context key should provide type-safe accessors
	// for the values stored using that key:
	//
	// 	// Package user defines a User type that's stored in Contexts.
	// 	package user
	//
	// 	import "context"
	//
	// 	// User is the type of value stored in the Contexts.
	// 	type User struct {...}
	//
	// 	// key is an unexported type for keys defined in this package.
	// 	// This prevents collisions with keys defined in other packages.
	// 	type key int
	//
	// 	// userKey is the key for user.User values in Contexts. It is
	// 	// unexported; clients use user.NewContext and user.FromContext
	// 	// instead of using this key directly.
	// 	var userKey key
	//
	// 	// NewContext returns a new Context that carries value u.
	// 	func NewContext(ctx context.Context, u *User) context.Context {
	// 		return context.WithValue(ctx, userKey, u)
	// 	}
	//
	// 	// FromContext returns the User value stored in ctx, if any.
	// 	func FromContext(ctx context.Context) (*User, bool) {
	// 		u, ok := ctx.Value(userKey).(*User)
	// 		return u, ok
	// 	}
	Value(key any) any  【这是一个函数声明,函数名是Value 参数名是key,参数类型是any接口,返回类型是any接口】
}

参考:

2、

标签:Context,网络协议,when,returns,Done,context,数据结构,closed
From: https://www.cnblogs.com/sybil-hxl/p/17078866.html

相关文章

  • 掌握hashtable,深度理解python字典的数据结构
    文章目录​​1.hash函数​​​​2.hashtable​​​​2.1链地址法实现hashtable​​​​2.2解决冲突​​​​2.3开放寻址法实现hashtable​​​​2.4逻辑删除key​​​......
  • python实用小技之数据结构
     本文大多数例子搬自pythoncookbook这里是对学习的一个总结和提炼ps:python版本为python3 1.解压序列赋值给多个变量#有一个包含N个元素的元组或者是序列,怎样将......
  • 【数据结构和算法】Trie树简介及应用详解
    作者:京东物流马瑞1什么是Trie树1.1Trie树的概念Trie树,即字典树,又称单词查找树或键树,是一种树形结构,典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以......
  • [数据结构]二叉树的前中后序遍历(递归+迭代实现)
    二叉树的遍历主要的三种遍历方式二叉树主要的遍历方式有前序遍历、中序遍历和后序遍历。(1)前序遍历:根节点-->左子树-->右子树(2)中序遍历:左子树-->根节点-->右子树(3)后序......
  • C/C++晋中理工学院数据结构[2023-01-30]
    C/C++晋中理工学院数据结构[2023-01-30]晋中理工学院数据结构实验周任务书2022-2023学年第1学期学院: 信创与大数据学院专业: 学生姓名: 学号......
  • 怎么高效学习数据结构和算法?
    5个步骤:基础语法学习—>语法配套练习—>数据结构—>算法入门—>算法进阶一、数据结构官方解释:​​数据结构​​是一门研究非数值计算的程序设计问题中的额操作对象,以及他们......
  • 【算法与数据结构】Trie树简介及应用
    作者:京东物流马瑞1什么是Trie树1.1Trie树的概念Trie树,即字典树,又称单词查找树或键树,是一种树形结构,典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常......
  • C语言算法与数据结构[2023-01-29]
    C语言算法与数据结构[2023-01-29]算法与数据结构大作业(2022—2023学年第1学期)学院电子信息工程学院专业班级电信20-2班学号202005010209......
  • C语言数据结构与算法分析课程设计题目[2023-01-29]
    C语言数据结构与算法分析课程设计题目[2023-01-29]2021-2022学年第一学期数据结构与算法分析课程设计题目课程设计总体要求:课程设计报告撰写内容包括:题目分析;概要设......
  • c++针对特定数据结构创建堆
    make_heaphttps://en.cppreference.com/w/cpp/algorithm/make_heapstructds{ doublevalue; intidx; ds(doublev,intindex):value(v),idx(index){}......