Let's GO(一)
近来开始学Go,留此博客以记录学习过程,顺便鞭策自己更加努力。
人生苦短,Let's GO!
简单介绍
The Go Programming Language
Go(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。
我学习主要参考七米老师的博客李文周的博客以及他在B站的视频,在此也感谢一下大佬无私分享。
今天我学了什么有趣的东西?
1.万物起源HelloWorld
package main //一个Go项目必须有一个main包
import "fmt"
func main() {
//万物起源Hello World
fmt.Println("Hello World!")
}
2.iota
const (
a1 = iota //a = iota == 0
b1 //b = iota == 1
c1 //c = iota == 2
)
3.string
//保留分行
s3 := ` Hello
World`
fmt.Println(s3)
/*
Hello
World
*/
4.array
//初始化按下标定义
str := [...]string{1:"java",3:"go",7:"c"}
fmt.Println(str)
//[java go c]
哦差点忘了,数组不可变!
5.slice
解决数组不可变
slice是引用数组的某一部分,改变slice会改变对应数组
s3 := make([]int,5,10)
fmt.Printf("%v,len:%v,cap:%v",s3,len(s3),cap(s3))
//slice能跟nil(空)比较,但不能跟其他slice比较
if s == nil {//suggest len(s) == 0
fmt.Println("s is nil")
}
//! not nil after init,although it is empty
var s4 = []int{} //or s4 := make([]int,0) the same
if s4 == nil {
fmt.Println("s4 is nil") //you won't see it
}
for i:=0; i<10; i++ {
s = append(s, i) //向s末尾添加值i的元素,容量不足会自动扩大
fmt.Printf("%v,len:%v,cap:%v,ptr:%p\n",s,len(s),cap(s),s)
}
//append s1(另一个切片)
s = append(s,s1...)
fmt.Println(s)
//delete index: append(s[0:index],s[index+1:]...)
s8 := append(s[0:2],s[3:]...) //delete s[2]
fmt.Println(s8)
//sort array
var b = [...]int{8,23,12,4,5}
sort.Ints(b[:])
fmt.Println(b)
6.if for switch
go 没有while,或者说,for expr {}就是while
//if
if i:=0; i>1 { //选择性定义
fmt.Println(i)
} else if i>2 {
fmt.Println(i)
} else {
fmt.Println(i)
}
//switch
switch str:="hello";str { //不仅支持整形
case "he"+"llo": //case可以使用表达式
fmt.Println("true")
fallthrough //go每个case自动break,使用fallthrough执行下一case
default:
fmt.Println("false")
}
//for
//1.
for i:=0; i<10; i++ {
fmt.Println(i)
}
//2.while
i := 10
for i>0 {
fmt.Println(i)
i--
}
//for range
var name = []int {1, 2, 3, 4, 5}
for j,k := range name {
fmt.Println(j,k)
}
7.指针
学过C的同学可能看到指针就会有点头皮发麻吗哈哈,go的指针很简便了
a := 10
pa := &a
fmt.Printf("pa:%v,addr:%p,type:%T\n",*pa,pa,pa)
就这么简单的用法,*和&,没有偏移
//还有感觉不太会用到的new
b := new(int) //*b = 0
fmt.Println(*b)
总而言之
Go还是挺有趣的,语法中充满了一些对语法老前辈的不满哈哈。
那么今天就学到这了,人生苦短,Let's GO!
随机推荐
- CentOS7——初始化
CentOS7--初始化 #禁止关闭显示器 archlinux wiki 提及的方法 echo -ne "\033[9;0]" >> /etc/issue # 重启,c ...
- Dedecms 修改当前位置样式
当前列表页间隔符样式修改 后台-->系统基本参数-->核心设置-->栏目位置的间隔符号 只有二级位置栏目时的间隔符去除 找到include文件中的typelink.class.php ...
- 【前端】仿消息推送到App提示
效果: JS: (function ($) { $.fn.loopmsg = function (options, param) { if (typeof options == 'string') ...
- FastJson将Java对象转换成json
确保环境依赖都配置好! 1.在pom.xml导入依赖 <dependency> <groupId>com.alibaba</groupId> <artifac ...
- 「雅礼集训 2017 Day4」洗衣服
题目 点这里看题目. 分析 首先考虑只有洗衣机的情况.我们可以想到,当前洗衣任务结束越早的洗衣机应该被先用,因此可以用堆来动态维护. 再考虑有烘干机的情况.很显然,越晚洗完的衣服应该越早烘 ...
- WebDriverWait
显示等待 WebDriverWait( driver, timeout, poll_frequency, ignored_exceptions) driver: 传入WebDriver实例,即webd ...
- 2、Redis如何配置成一个windows服务并且设置一键安装卸载与启停
每天启动redis虽然只是一个命令行的事情,但是还是比较烦,所以…… 参考文档:Windows Service Documentation.docx 默认前提:Redis已安装并配置完成(不知道如何配 ...
- cocos2dx 启用cjson
在appDelegate.cpp文件中添加 #if __cplusplus extern "C" { // 加入此代码的目的,防止污染引擎的scripting目录 #include ...
- 【Key】 Windows 95 到 Windows10所有KEY 包括OEM系统
部分可能不准确,请见谅,数据源自Baidu,由noogai00整理,数据为Microsoft.所有 Windows 95 02398-OEM-0030022-5886409297-OEM-002128 ...
- Ubuntu16.06常见服务搭建
摘要 系统环境Ubuntu 16.04 amd64 隔一段时间要配一次服务记不住,记录在这里方便以后安装. 目前更新了以下服务: ssh samba vimrc // 20200126更新 ssh 安 ...