字符串
package main
import("fmt"
"strings"
"strconv"
)
func main(){
var str string = "hello hello world abc"
////是不是以某个字符串开头
cc:=strings.HasPrefix(str,"http://")
fmt.Println(cc)
//是不是以某个字符串结尾
dd:=strings.HasSuffix(str,"http://")
fmt.Println(dd)
//判断str在s中首次出现的位置,如果没有返回-1
ee:=strings.Index(str,"he")
fmt.Println(ee)
ff:=strings.LastIndex(str,"abc")
fmt.Println(ff)
//将hello替换成go 2次
result := strings.Replace(str,"hello","go",2)
fmt.Println(result)
//求出包含go的次数
countTime := strings.Count(result,"go")
fmt.Println(countTime)
//重复count次abc
repeatResult := strings.Repeat("abc",2)
fmt.Println(repeatResult)
//转为大小写
result = strings.ToUpper(result)
fmt.Println(result)
result = strings.ToLower(result)
fmt.Println(result)
//去掉首尾空格
result = strings.TrimSpace(result)
fmt.Println(result)
//去掉字符串首尾cut字符
result = strings.Trim(result,"g")
fmt.Println(result)
//去掉字符串首cut字符
result = strings.TrimLeft(result,"o")
fmt.Println(result)
//去掉字符末尾cut字符
result = strings.TrimRight(result,"c")
fmt.Println(result)
//返回str空格分隔的所有子串的slice,
resultSlic := strings.Fields(result)
fmt.Println(resultSlic)
//返回str split分隔的所有子串的slice
resultSlic = strings.Split(result," ")
fmt.Println(resultSlic)
//用sep把s1中的所有元素链接起来
result = strings.Join(resultSlic,"l")
fmt.Println(result)
//数字转字符串
a:=strconv.Itoa(100)
fmt.Println(a)
//字符串转数字
b,err:=strconv.Atoi("100")
if err != nil{
fmt.Println("c` n't convert to int",err)
return
}
fmt.Println("number:",b)
}
输出如下:
日期时间
package main
import("fmt"
"time"
)
func main(){ //输出当前日期时间
now := time.Now()
fmt.Println(now.Format("2006/01/02 15:04:05"))
//输入程序运行的毫秒值
start := time.Now().UnixNano()
testDealy()
end := time.Now().UnixNano()
fmt.Printf("%d",(end - start)/1000)
}
func testDealy(){
time.Sleep(time.Millisecond*100)
}
输出如下:
2017/10/22 14:10:16 100067
指针
普通类型,变量存的就是值,也叫值类型
获取变量的地址,用&,比如: var a int,获取a的地址:&a
指针类型,变量存的是一个地址,这个地址存的才是值
获取指针类型所指向的值,使用:*,比如:var *p int,使用*p获取p指向的值
package main
import("fmt"
)
func main(){
var a int = 10
var p *int
p = &a
fmt.Println(a,&a,*p,p)
*p = 9
fmt.Println(a,p)
var b int = 10
p = &b
*p = 11
fmt.Println(a,p,b,&b)
}
输出如下:
10 0xc04203a1c0 10 0xc04203a1c0 9 0xc04203a1c0 9 0xc04203a1c0 9 0xc04203a1c0 11 0xc04203a210 11 0xc04203a210
简单分析下将a的内存地址值,赋值给p,这样a和p内存地址值是一样的,0xc04203a1c0,修改*p=9,这样a,p的内存地址值值都变成9,但是a,p的内存地址没有改变,依然是0xc04203a1c0,
然后讲b的内存地址值,赋值为p,这样pb内存地址就一样了,内存地址值指向的值都是11
switch
package main
import("fmt"
)
func main(){
var a int = 1
switch a {
//如果a==0时候,不输出
case 0:
case 1,2:
fmt.Println("a == 1,2")
//会执行case3
fallthrough
case 3:
fmt.Println("a == 3")
case 4:
fmt.Println("a == 4")
default:
fmt.Println("default")
}
//写法二
switch{
case a>0:
fmt.Println("a>0")
case a<0:
fmt.Println("a<0")
default:
fmt.Println("defalut")
}
}
输出如下:
a == 1,2 a == 3 a>0
for
package main
import("fmt"
)
func main(){
//第一种for
for i := 0 ; i < 5; i++ {
fmt.Printf("i=%d\n",i)
}
//第二种for
var a int
for true{
fmt.Println("a",a)
a+=1
if a>3{
break
}
}
//第三种for
var b int
for{
b+=1
if b<3{
continue
}
fmt.Println("b",b)
if b>5{
break
}
}
//第四种for
var str string = "hello"
for i,v := range str{
fmt.Printf("index[%d] val[%c]",i,v)
}
}
输出:
i=0
i=1
i=2
i=3
i=4
a 0
a 1
a 2
a 3
b 3
b 4
b 5
b 6
index[0] val[h]index[1] val[e]index[2] val[l]index[3] val[l]index[4] val[o]
goto、label
package main
import("fmt"
)
func main(){
var a int = 1
//LABEL标签
LABEL1:
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if j == 2 {
continue LABEL1
}
fmt.Printf("i is: %d,and j is: %d\n",j)
}
}
//HERE标签
HERE:
a+=1
if a==5{
fmt.Println("a == 5")
}
goto HERE
}
输出如下:
i is: 0,and j is: 0
i is: 0,and j is: 1
i is: 1,and j is: 0
i is: 1,and j is: 1
i is: 2,and j is: 0
i is: 2,and j is: 1
a == 5