Learn Go 是在codecademy上的Go语言基础课程,这週的主要内容在学习function的写法,以及指标参数
Learn Go in 12 Minutes,这一个影片可以用来检视对Go语言的基础认识
学习笔记Go
五、Go Functions
func myFunciont(参1, 参2 参数类型) (回传类型1, 回传类型2 ){}, (Go函式定义方式)需要注意作用域,分为全域变数与区域变数parameters(可以多个输入与多个回传)()defer keyword. 在函式结束时执行。由于函式结束有多种方式,因此这样写可以确保在函式最后执行,This is useful for logging, file writing, and other utilities.Go is a pass-by-value language.(一般变数在函式中是複製一个值进行运算,不过指标变数由于存取地址,可以直接改原本的值。相较于Pass by reference)An address is where a value is stored. (记忆体地址以十六进位表示)use the & operator to find an address of a variable(取址)Pointers are variables that store an address.A pointer is specific to what type of address it can store.(指标不能指给不同类型的值,宣告*int时,就只能指派int值给它)The * operator can be used to assign a pointer the type of the value its address holds.The * operator can also be used to dereference a pointer and assign a new value.练习题目:
题目一:
已知感测器资料储存方式为字串,每个char代表一个感测数值
sensor ="000000000000000000000000000000"
已知有一个int阵列 [1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3]
请写一个function 输入int陈列回传相对应的sensor字串
//阵列转换为字串 //输入[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3] //输出"123123123123123123123123123123" func parseeIntArrayToSensorStr(data int[30]) string{}
题目二:
不同于第一题,设计一个function,传入指标
function应只回传error,
在function内需要修改sensor的字串,
将其中所有偶数变为0
//"123123123123123123123123123123"//执行function 后print out://"103103103103103103103103103103" func changeOriginStr(str *string) error { }