无法使用接口添加带有中文的上下文值。

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

Cant add Contex Value with Chi from interface

问题

我有来自接口的数据,并且我将其添加到上下文中。根据文档,使用context.WithValue(r.Context(), key, val)可以实现。

我尝试添加一个键和值是可以的,但是如果超过一个就不行了。

result := map[string]interface{}{}
encoded, _ := json.Marshal(data)
json.Unmarshal(encoded, &result)
fmt.Println("data claim \n", result)
var ctx context.Context
for key, val := range result {
    fmt.Printf("key %v value %v \n", key, val)
    ctx = context.WithValue(r.Context(), key, val)
}
fmt.Println("ctx middleware \n", ctx)

在结果中,我有多个数据,但只有一个数据起作用。

英文:

i have data from interface, and i will add on context. from documentation is context.WithValue(r.Context(), key, val)

i try to add 1 key and value is work, but can't work if more than 1.

		result := map[string]interface{}{}
		encoded, _ := json.Marshal(data)
		json.Unmarshal(encoded, &result)
		fmt.Println("data claim \n", result)
		var ctx context.Context
		for key, val := range result {
			fmt.Printf("key %v value %v \n", key, val)
			ctx = context.WithValue(r.Context(), key, val)
		}
		fmt.Println("ctx middleware \n", ctx)

on result i have more than 1 data, but only 1 data is work.

答案1

得分: 1

请尝试这样做:

result := map[string]interface{}{}
encoded, _ := json.Marshal(data)
json.Unmarshal(encoded, &result)
fmt.Println("data claim \n", result)
ctx := r.Context()
for key, val := range result {
    fmt.Printf("key %v value %v \n", key, val)
    ctx = context.WithValue(ctx, key, val)
}
fmt.Println("ctx middleware \n", ctx)

看起来你一直在使用r.Context(),但没有使用更新后的版本。

英文:

Could you please try this

        result := map[string]interface{}{}
        encoded, _ := json.Marshal(data)
        json.Unmarshal(encoded, &result)
        fmt.Println("data claim \n", result)
        ctx:=r.Context()
        for key, val := range result {
            fmt.Printf("key %v value %v \n", key, val)
            ctx = context.WithValue(ctx, key, val)
        }
        fmt.Println("ctx middleware \n", ctx)

Seems you are using r.Context() all the time but not the updated one.

答案2

得分: 0

由于您在上下文键中使用了string类型,这个问题已经在上下文文档中提到过了。

提供的键必须是可比较的,并且不应该是字符串类型或任何其他内置类型,以避免在使用上下文的包之间发生冲突。

以下是在上下文中获取和设置键/值的示例代码:

package main

import (
	"context"
	"log"
)

// 您需要定义一个类型来防止冲突
type key int

const fooKey key = 1
const barKey key = 2

func main() {

	ctx := context.Background()
	m := map[key]string{
		fooKey: "foo",
		barKey: "bar",
	}

	// 添加到上下文中
	for k, v := range m {
		ctx = context.WithValue(ctx, k, v)
	}

	// 从上下文中获取值
	for k := range m {
		v := ctx.Value(k).(string)
		log.Println("value is: ", v)
	}
}
英文:

Since you're using the type string for the context key. This problem is already mentioned in the context document.

> The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context

Here is the example for getting and setting key/value in context

package main

import (
	"context"
	"log"
)

// You need to define a type to prevent the collision
type key int

const fooKey key = 1
const barKey key = 2

func main() {

	ctx := context.Background()
	m := map[key]string{
		fooKey: "foo",
		barKey: "bar",
	}

	// Add to context
	for k, v := range m {
		ctx = context.WithValue(ctx, k, v)
	}

	// Get value from context
	for k := range m {
		v := ctx.Value(k).(string)
		log.Println("value is: ", v)
	}
}

huangapple
  • 本文由 发表于 2022年3月9日 16:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/71406080.html
匿名

发表评论

匿名网友

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

确定