php框架做网站,阿里云建设网站视频,wordpress插件直播,做网站建设工资高吗Go基础#xff08;待更新#xff09;
参考Go 语言教程 文章目录 Go基础#xff08;待更新#xff09;一、基本语法1、格式化输出2、声明并赋值1#xff09;单变量赋值2#xff09;多变量赋值 二、math工具包的使用三、函数1、参数传递1#xff09;普通传递2#xff09…Go基础待更新
参考Go 语言教程 文章目录 Go基础待更新一、基本语法1、格式化输出2、声明并赋值1单变量赋值2多变量赋值 二、math工具包的使用三、函数1、参数传递1普通传递2指针传递 四、类型转换五、面向对象1、结构体2、封装3、继承4、多态 六、集合1、List1遍历list中的对象 一、基本语法
1、格式化输出
参考
go语言中的输出语句Go语言 - fmt包 | 输出 | 占位符
格式化输出printf(%v mod %v %v,a,b,c)
package mainimport fmtfunc main(){fmt.Print(My weight on the surface of Mars is )fmt.Print(149.0 * 0.3783)fmt.Print( lbs, and I would be )fmt.Print(41 * 365 / 687)fmt.Print( years old.)fmt.Println()fmt.Println(My weight on the surface of Mars is, 149.0 * 0.3783, lbs, and I would be, 41 * 365.2425/687, years old.)// 格式化打印fmt.Println(------------------------------------格式化打印--------------------------------)fmt.Printf(My weight on the surface of Mars is %v lbs,, 149.0 * 0.3783)fmt.Printf( and I would be %v years old.\n, 41 * 365 / 687)fmt.Printf(My weight on the surface of %v is %v lbs.\n, Earth, 149.0)fmt.Println(-------------使用Printf对齐文本-----------)fmt.Printf(%-15v $%4v\n, SpaceX, 94)fmt.Printf(%-15v $%4v\n, Virgin Galactic, 100)}
// Print、Println函数可以传递若干个参数参数之间可以用逗号隔开
// 参数可以是字符串、数字、数学表达式等
// 格式化打印可以使用Printf来控制打印的输出结果与Print和Println不同Printf的第一个参数必须是字符串。// 这个字符串中包含了像%v这样的格式化动词它的值由第二个参数的值所代替。// 如果指定了多个格式化动词那么它们的值由后面的参数值按顺序进行替换。// 使用Printf对齐文本// 在格式化动词中指定宽度就可以对齐文本。例如%4v就是向左填充足够4个宽度// 正数向左填充空格// 负数向右填充空格
---
My weight on the surface of Mars is 56.3667 lbs, and I would be 21 years old.
My weight on the surface of Mars is 56.3667 lbs, and I would be 21.79758733624454 years old.
------------------------------------格式化打印--------------------------------
My weight on the surface of Mars is 56.3667 lbs, and I would be 21 years old.
My weight on the surface of Earth is 149 lbs.
-------------使用Printf对齐文本-----------
SpaceX $ 94
Virgin Galactic $ 1002、声明并赋值
参考go语言的变量声明并赋值运算符(:)
1单变量赋值
eg1 第8行声明并赋值变量a失败因为变量a已经在第6行声明过了。
$ cat main.c
package mainimport fmtfunc main() {var a int 100fmt.Printf(a%p\n, a);a : 100 //报错fmt.Printf(a%p\n, a);
}$ go build main
# main
./main.go:8: no new variables on left side of :eg2 虽然在第5行有声明了一个全局变量a在第9行依然可以声明并赋值变量a此时的a和全部变量a不是一个变量打出来的地址不相同。
$ cat main.c
package mainimport fmtvar a int 100func main() {fmt.Printf(a%p\n, a);a : 100 fmt.Printf(a%p\n, a);
}$ go build main
a0x4f8018
a0xc42000e2682多变量赋值
eg3 第10行已经声明了变量a**第12行就不在重新定义变量a只定义了变量b**可以看到两个a打印出来的是同一个。
$ cat main.c
package mainimport fmtfunc foo() (int, int) {return 100, 200
}func main() {var a int 0;fmt.Printf(a%p\n, a);a, b : foo()fmt.Printf(a%p, b%p\n, a, b);
}$ go build main
a0xc42000e260
a0xc42000e260, b0xc42000e280二、math工具包的使用
参考go语言基础 数学包 math
package mainimport (fmtmath
)func main() {/*math包*/i : -100fmt.Println(math.Abs(float64(i))) //绝对值fmt.Println(math.Ceil(5.0)) //向上取整fmt.Println(math.Floor(5.8)) //向下取整fmt.Println(math.Mod(11, 3)) //取余数同11%3fmt.Println(math.Modf(5.26)) //取整数取小数fmt.Println(math.Pow(3, 2)) //x的y次方fmt.Println(math.Pow10(4)) // 10的n次方fmt.Println(math.Sqrt(8)) //开平方fmt.Println(math.Cbrt(8)) //开立方fmt.Println(math.Pi)
}三、函数
参考 Go 语言函数 go语言函数参数传递详解
1、参数传递
1普通传递
eg1
package main
import (fmt
)
func swap(a int, b int) {var temp inttemp aa bb temp
}
func main() {x : 5y : 10swap(x, y)fmt.Print(x, y)
}
---
输出结果5 10eg2
/* 函数返回两个数的最大值 */
func max(num1, num2 int) int {/* 声明局部变量 */var result intif (num1 num2) {result num1} else {result num2}return result
}2指针传递
函数的变量不仅可以使用普通变量还可以使用指针变量
package main
import (fmt
)
func swap(a *int, b *int) {var temp inttemp *a*a *b*b temp
}
func main() {x : 5y : 10swap(x, y)fmt.Print(x, y)
}
---
输出结果10 5四、类型转换
参考Go 语言类型转换
go 不支持隐式转换类型比如 :
package main
import fmtfunc main() { var a int64 3var b int32b afmt.Printf(b 为 : %d, b)
}
此时会报错cannot use a (type int64) as type int32 in assignment
cannot use b (type int32) as type string in argument to fmt.Printf但是如果改成 b int32(a) 就不会报错了:
package main
import fmtfunc main() { var a int64 3var b int32b int32(a)fmt.Printf(b 为 : %d, b)
}五、面向对象
参考Go语言没有类怎么面向对象
1、结构体
https://www.runoob.com/go/go-structures.html
package mainimport fmttype Books struct {title stringauthor stringsubject stringbook_id int
}func main() {// 创建一个新的结构体fmt.Println(Books{Go 语言, www.runoob.com, Go 语言教程, 6495407})// 也可以使用 key value 格式fmt.Println(Books{title: Go 语言, author: www.runoob.com, subject: Go 语言教程, book_id: 6495407})// 忽略的字段为 0 或 空fmt.Println(Books{title: Go 语言, author: www.runoob.com})
}2、封装
type Employee struct {Name stringSex stringAge int
}func (e *Employee) ToString() string {return name e.Name ;sex e.Sex ;age strconv.Itoa(e.Age)
}3、继承
Go里面也没有像Java中类似extend继承的语法Go是用了类似Java里组合的东西来让语法看起来像继承
type TechEmployee struct {EmployeeSalaryPerMonth float32
}type SaleEmployee struct {EmployeeBaseSalary float32ExtraRate float32
}对应的实例化和使用
//实例化时是传了个employee
tech : object.TechEmployee{Employee: object.Employee{Name: lee},SalaryPerMonth: 10000,
}
//这里看起来像拥有了Employee的name属性可以设置和访问
tech.Name bruce lee
fmt.Println(tech.Name)4、多态
type TechEmployee struct {EmployeeSalaryPerMonth float32
}func (e *TechEmployee) CalcSalary() float32 {return e.SalaryPerMonth
}type Machine struct {}func (e *Machine) CalcSalary() float32 {return 0
}在Java里比较直观语法里直接写着实现xxx接口Go相比的话没那么直观但更灵活它没有指定实现哪个接口而是如果定义了一个相同名字和返回值的方法就认为是实现了对应拥有这个方法的接口这里假如接口有两个方法对应也必须要两个方法都有定义了才认为是实现了接口。
六、集合
1、List
参考Go标准容器之List
package mainimport (container/listfmt
)func main() {nums : list.New()nums.PushBack(1)nums.PushBack(2)nums.PushBack(3)for e : nums.Front(); e ! nil; e e.Next() {fmt.Println(e.Value)}
}1遍历list中的对象
参考关于go语言集合中Element转换为自定义结构体的方法
Element中Value存放的是接口所以只要将接口转换为结构体问题就都解决了其中conn为返回的结构体而OK是布尔型表示有没有转换成功,true为成功。
conn , ok : (i.value).(net.Conn)eg
for e : testCases.Front(); e ! nil; e e.Next() {fmt.Printf(#################### testcase%v ##################\n,i)t : (e.Value).(testCase)A_w,A_j,B_w,B_j,L,bearing : t.A_w,t.A_j,t.B_w,t.B_j,t.L,t.bearing
}
相关文章: