mgo 批量插入--具体类型的切片 转 interface 切片解决办法

通过xml.Unmarshal获得一个[]Province 然后想把它插到mongodb中,使用的是mgo driver。 那么怎样将[]Province切片转为[]interface{}切片呢?

法1.

 c := session.DB(database).C("Provinces")
 var provinces []Province
 var docs []interface{}
  ...
 for _,v := range provinces{
    docs = append(docs,v)
 }
 c.Insert(docs...)

法2.

package main

import (
    "fmt"
    "reflect"
)

type a struct {
    CreateTime int
    Name       string
    Content    string
}

func main() {
    arr1 := []a{
        a{Name: "no1", CreateTime: 1, Content: "hello1"},
        a{Name: "no2", CreateTime: 2, Content: "hello2"},
        a{Name: "no3", CreateTime: 3, Content: "hello3"},
        a{Name: "no4", CreateTime: 4, Content: "hello3"},
    }

    fmt.Println(arr1)

    arr2 := ToInterfaceArr(arr1)

    fmt.Printf("%T\n", arr2)
    fmt.Println(arr2)
}

func ToInterfaceArr(arr interface{}) []interface{} {
    if reflect.TypeOf(arr).Kind() != reflect.Slice {
        return nil
    }

    arrValue := reflect.ValueOf(arr)
    retArr := make([]interface{}, arrValue.Len())
    for k := 0; k < arrValue.Len(); k++ {
        retArr[k] = arrValue.Index(k).Interface()
    }
    return retArr
}

 

如何将多维切片[] interface {}换为切片[] string?

01-22
<div class="post-text" itemprop="text"> <p>Example source:</p> <pre><code>// source multidimensional slice var source = []interface{}{ "value1", "value2", 1234, 1234.1234, []int{222, 333}, []float32{444.444, 555.555}, []interface{}{555, "value4", []int{777, 888}} } </code></pre> <p>Target:</p> <pre><code>// target []string var target = []string{ "value1", "value2", "1234", "1234.1234", "222", "333", "444.444", "555.555", "555", "value4", "777", "888" } </code></pre> <p>I wrote the conversion function. But it seems to me cumbersome and it does not cover all possible options. Could you tell me please can be there is more elegant decision?</p> <pre><code>// convert multidimensional slice []interface{} to slice []string func convert(raw interface{}) (result []string) { switch raw.(type) { case []string: result = append(result, raw.([]string) ...) case []int: for _, elem := range raw.([]int) { result = append(result, convert(elem) ...) } case string, int, int8, int16, int32, int64, float32, float64, complex64, complex128: result = append(result, fmt.Sprintf("%v", raw)) case []float32: for _, elem := range raw.([]float32) { result = append(result, convert(elem) ...) } case []rune: for _, elem := range raw.([]rune) { result = append(result, convert(elem) ...) } case []int8: for _, elem := range raw.([]int8) { result = append(result, convert(elem) ...) } case []int16: for _, elem := range raw.([]int16) { result = append(result, convert(elem) ...) } case []int64: for _, elem := range raw.([]int64) { result = append(result, convert(elem) ...) } case []float64: for _, elem := range raw.([]float64) { result = append(result, convert(elem) ...) } case []complex64: for _, elem := range raw.([]complex64) { result = append(result, convert(elem) ...) } case []complex128: for _, elem := range raw.([]complex128) { result = append(result, convert(elem) ...) } case []interface{}: for _, elem := range raw.([]interface{}) { result = append(result, convert(elem) ...) } case [][]interface{}: for _, elem := range raw.([][]interface{}) { result = append(result, convert(elem) ...) } default: fmt.Println(fmt.Sprintf("Unknown type %v", raw)) os.Exit(1) } return result } </code></pre> </div>
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值