项目中需要用到golang的队列,container/list,需要放入的元素是struct,但是因为golang中list的设计,从list中取出时的类型为interface{},所以需要想办法把interface{}转换为struct。

这里需要用到interface assertion,具体操作见下面代码:

 package main

 import (
"container/list"
"fmt"
"strconv"
) type People struct {
Name string
Age int
} func main() {
// Create a new list and put some numbers in it.
l := list.New()
l.PushBack(People{"zjw", }) // Iterate through list and print its contents.
e := l.Front()
p, ok := (e.Value).(People)
if ok {
fmt.Println("Name:" + p.Name)
fmt.Println("Age:" + strconv.Itoa(p.Age))
} else {
fmt.Println("e is not an People")
}
}

golang将interface{}转换为struct的更多相关文章

  1. golang 关于 interface 的学习整理

    Golang-interface(四 反射) go语言学习-reflect反射理解和简单使用 为什么在Go语言中要慎用interface{} golang将interface{}转换为struct g ...

  2. golang中接口interface和struct结构类的分析

    再golang中,我们要充分理解interface和struct这两种数据类型.为此,我们需要优先理解type的作用. type是golang语言中定义数据类型的唯一关键字.对于type中的匿名成员和 ...

  3. golang 中 map 转 struct

    golang 中 map 转 struct package main import ( "fmt" "github.com/goinggo/mapstructure&qu ...

  4. Golang的Interface是个什么鬼

    问题概述 Golang的interface,和别的语言是不同的.它不需要显式的implements,只要某个struct实现了interface里的所有函数,编译器会自动认为它实现了这个interfa ...

  5. golang的interface剖析

      背景: golang的interface是一种satisfied式的.A类只要实现了IA interface定义的方法,A就satisfied了接口IA.更抽象一层,如果某些设计上需要一些更抽象的 ...

  6. 匿名字段 内嵌结构体 interface作为struct field 匿名接口

    interface作为struct field,谈谈golang结构体中的匿名接口 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles/19 ...

  7. Golang 的 `[]interface{}` 类型

    Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...

  8. golang(10)interface应用和复习

    原文链接 http://www.limerence2017.com/2019/10/11/golang15/ interface 意义? golang 为什么要创造interface这种机制呢?我个人 ...

  9. Golang接口(interface)三个特性(译文)

    The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...

随机推荐

  1. Debug 路漫漫-04

    1.错误使用 cat 要串联的数组的维度不一致. ——前面给个初始化即可: D = cell(length(trainIdx),1); user_itemData = cell(length(trai ...

  2. iptables中ULOG和NFLOG实现分析【转】

    原文地址:http://blog.csdn.net/eydwyz/article/details/52456335 ----------原文如下---------------------------- ...

  3. 【ASP.NET】第一个ASP.NET MVC应用程序

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 开发流程 新建Controller 创建Action 根据Action创建View 在Action获取数据并生产ActionResult传递 ...

  4. LIGHT OJ 1199 - Partitioning Game

    传送门 1199 - Partitioning Game    PDF (English) problem=1199" style="color:rgb(79,107,114)&q ...

  5. 适合移动端与PC端的 CSS Reset - m_base.css

    文章来源:http://www.cnblogs.com/HCJJ/p/6399185.html 具体代码 @charset "utf-8"; html { -ms-text-siz ...

  6. Desugar Scala(17) -- Option和for,以及脑子里发生的事情

    欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/08/30/options-for/ Scala里的forkeyword是个非常有趣的东西. 能够用来把多层嵌套f ...

  7. springboot 多环境配置yml或properties

    https://www.cnblogs.com/mr-yang-localhost/p/8971327.html   springboot 多环境配置 https://blog.csdn.net/li ...

  8. ELK日志相关

    转: Logstash 讲解与实战应用 原创qw871122016-08-20 16:06:07评论(1)40217人阅读 一.Logstash 介绍 Logstash 是一款强大的数据处理工具,它可 ...

  9. 基于Docker搭建LNMP环境(转)

    关于什么是docker,建议大家先上网查查有关的用法.如果您不了解,在这篇文章中,您可以简单的理解为他是一个轻量级的虚拟机. 一.docker安装mysql 首先,我们从仓库拉取一个MySql的镜像 ...

  10. uiautomatorviewer 查看元素新思路

    用adb 命令把图片和uix获取出来,再导入uiautomatorviewer adb shell uiautomator dump /data/local/tmp/uidump.uixadb pul ...