搭建最简单的区块结构

时间:2020-04-07 00:29:35   收藏:0   阅读:64

最近准备用go搭一个简单的区块链项目,虽说有十分详细的教程,但是还是有些地方不太容易理解(个...个人原因?),下面是一个极其简易的区块结构,主要运用了go中结构体和切片的特性。

代码

// 结构体 + 切片 
// 后续学习的简易框架

package main

import "fmt"

type information struct {
	name string
	age  int
	home string
}

/* 		这一步很关键		*/
type list struct {
	informs []*information
}

func main() {
	var block list
	a := information{"猪猪", 13, "埃塞俄比亚"}
	b := information{"笨笨", 15, "阿尔及利亚"}
	c := information{"可爱", 16, "尼日利亚"}

	block.informs = append(block.informs, &a)
	block.informs = append(block.informs, &b)
	block.informs = append(block.informs, &c)
	
        //测试
	//fmt.Println(a)
	//fmt.Println(b)
        //fmt.Println(c)
	//fmt.Println(block.informs)

	for _, i := range block.informs {
		fmt.Printf("姓名: %s\n", i.name)
		fmt.Printf("年龄: %d\n", i.age)
		fmt.Printf("家住在:%s\n", i.home)
		fmt.Println()
	}
}

运行结果:

姓名: 猪猪
年龄: 13
家住在:埃塞俄比亚

姓名: 笨笨
年龄: 15
家住在:阿尔及利亚

姓名: 可爱
年龄: 16
家住在:尼日利亚
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!