golang 方法参数为interface 怎么引用?

studyseo · 2020-04-17 15:02:30 · 3779 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2020-04-17 15:02:30 的主题,其中的信息可能已经有所发展或是发生改变。

package main

import "fmt"

func main() {
    a2 := "12435"
        //想用过这个函数过去 给他重复赋值
    a(&a2)
       // 
        // 这  a2 的值 为5 应该怎么做呢
    fmt.Println(a2)
}

func a(ap interface{})  {
    ap = 5
}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3779 次点击  
加入收藏 微博
12 回复  |  直到 2022-11-11 14:10:06
studyseo
studyseo · #1 · 5年之前
package main

import (
    "encoding/json"
    "fmt"
)

type s struct {

}
func main() {
    a2 := "12435"
    a(&a2)
    fmt.Println(a2)

    a1_str := map[string]string{
        "a":"233",
    }
    a1_map := map[string]string{}
    a1(a1_str,&a1_map)
    fmt.Println("map 值",a1_map)
}

func a1( s map[string]string,smap interface{})  {
    mb, _ := json.Marshal(s)
    json.Unmarshal(mb,&smap)
}
func a(ap interface{})  {
    ap = 5
}
//运行结果
12435
map 值 map[a:233]

忘解惑

studyseo
studyseo · #2 · 5年之前
12435
map 值 map[a:233]
studyseo
studyseo · #3 · 5年之前

看了哈源码 道行太浅

czyt
czyt · #4 · 5年之前

看了你的代码,我没看懂你是什么意图。

studyseo
studyseo · #5 · 5年之前
czytczyt #4 回复

看了你的代码,我没看懂你是什么意图。

就是封装mongodb 查询函数 第一个参数为

type FindConstructor struct {
    Where  bson.M
}

第二个参数 interface 这个用来接受查询数据的返回值

遇到的问题是 第二个内存地址换了导致 不会有数据返回 但是使用 json.Unmarshal 能达到要求 所有完整代码如下

func Find(cstor FindConstructor, data interface{}) (findstatus FindStatusCode) {
    d2 := []interface{}{}
    table_name := getCollName(data)
    find, err := db.Coll(table_name).Find(db.TimeOut(), cstor.Where)
    if err != nil {
        findstatus = checkMongoErr(err)
        return
    }
    err = find.All(db.TimeOut(), &d2)
    if err != nil {
        findstatus = checkMongoErr(err)
        return
    }
    var errbson error
    var temporaryBytes []byte
    var json_list = [][]byte{}
    for _, v := range d2 {
        temporaryBytes, errbson = bson.MarshalExtJSON(v, true, true)
        if errbson != nil {
            continue
        }
        json_list = append(json_list, temporaryBytes)
    }
    j := bytes.Join(json_list, []byte(","))
    j2 := "[" + string(j) + "]"
    err = json.Unmarshal([]byte(j2), &data)

    return
}
studyseo
studyseo · #6 · 5年之前

这是调用代码 感觉很不完善

package manage

import (
    "ask/dd"
    "ask/model"
    "ask/ot"
    "github.com/gin-gonic/gin"
)

func NodeList(c *gin.Context) {
    node_list := []model.Node{}
    dd.Find(dd.FindConstructor{}, &node_list)
    c.JSON(200, ot.Success(node_list, ""))
}
jarlyyn
jarlyyn · #7 · 5年之前

两个办法

第一个办法是类型强制转换,从,转为*string

第二个办法是反射

renjun0106
renjun0106 · #8 · 2年之前

func a(ap interface{}) { val := reflect.ValueOf(ap) if val.Kind() != reflect.Ptr { panic("some: check must be a pointer") } val.Elem().Set(reflect.ValueOf("5")) }

don178
don178 · #9 · 2年之前

func main() {
    a2 := "12435"
        //想用过这个函数过去 给他重复赋值
    a(&a2)
       // 
        // 这  a2 的值 为5 应该怎么做呢
    fmt.Println(a2)
}

func a(ap interface{})  {
    if newAp,ok := ap.(*string);ok{
        *newAp = "5"
    }
}
tuzhiya
tuzhiya · #10 · 2年之前

不同类型的话,只能用反射

mengyan
mengyan · #11 · 2年之前

最好的办法,就是断言

bsdx866
bsdx866 · #12 · 2年之前

你这个函数的接收参数是一个空接口,空接口可以接受很多类型的参数,那怎么知道你ap的类型?这个就得需要反射里面的类型断言,最后才能去设置这个变量的值。

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传