前言:
首先学习go是想在有java基础的情况下再学习一门新语言,了解了一些语言背景后发现go比较适合我。这里的学习笔记,主要是阅读《go入门指南》而记录的,只为能够快速上手。
Downloads - The Go Programming Language
这里我下的是window版zip包,完了解压。
这里要配置四个变量:
//开启module模式
GO111MODULE= on
//添加go代理网址,国内需要使用,这是七牛的
GOPROXY=https://goproxy.cn
//go工程所在目录
GOPATH=
//go的根目录
GOROOT=
package main
import (
"fmt"
"runtime"
)
func main() {
//打印出go的版本
fmt.Printf("%s\n", runtime.Version())
}
const Pi = 3.1415926
//显式定义
const b string = "abc"
//隐式定义
const b = "abc"
var a int = 15
var b bool = false
var str string = "hello"
// 显示定义在有些方面是必要的,如下
var n int64 = 2
// 在函数内,推荐使用更简洁的写法
a := 1
a, b, c := 4, 5, "hello"
// go的交换很好用
a, b = b, a
fmt.Printf("path is : %d\n %d\n %s\n", a, b, c)
都在testGo下
package instance
import (
"fmt"
)
func SayHello() {
fmt.Println("hello")
}
package main
import (
"fmt"
say "testGo/instance"
)
func main() {
fmt.Println("kobe")
say.SayHello()
}
优先级比main高,所以全局变量也可在init函数中声明
package trans
import (
"math"
)
var Pi float64
func init() {
Pi = 4 * math.Atan(1)
}
package main
import (
"fmt"
"testGo/trans"
)
var twoPi = 2 * trans.Pi
func main() {
fmt.Printf("2 * pi = %g\n", twoPi)
}
//bool类型 默认为false
var b bool = true
int 和 uint 在32位系统就是32位(4字节),64位系统就是64位
格式化说明
%d:整数(%x:格式化16进制数)
%g:格式化浮点型(%f浮点数,%e科学计数法)
%0d:用于输出定长整数
%n.mg表示一共占n个位置并精确到小数点后m位,除了g,还可以用e或f
安全的数值转换
func Uint8FromInt(n int) (uint8, error) {
if 0 <= n && n <= math.MaxUint8 {
// conversion is safe
return uint8(n), nil
}
return 0, fmt.Errorf("%d is out of the uint8 range", n)
}
随机数
//10以内的随机数
package main
import (
"fmt"
"math/rand"
)
func main() {
for i := 0; i <= 10; i++ {
a := rand.Intn(10)
fmt.Printf("%d\n", a)
}
}
//100以内的随机小数保留2个小数
package main
import (
"fmt"
"math/rand"
)
func main() {
for i := 0; i <= 10; i++ {
a := 100 * rand.Float32()
fmt.Printf("%2.2f\n", a)
}
}
complex64(32位实数和虚数)
complex128(64位实数和虚数)
package main
import "fmt"
func main() {
var c complex64 = 2 + 8i
fmt.Printf("%v\n", c)
}
严格来说,go语言中字符类型只是整数类型的特殊用例。byte类型是uint8的别名
package main
import (
"fmt"
)
func main() {
var ch1 byte = 'A'
var ch2 byte = 65
var ch3 byte = '\x41'
fmt.Printf("%c-%c-%c\n", ch1, ch2, ch3)
}
获取长度len()
获取第个位置字符str[n]
拼接字符串
//如java一般可以直接用+,但是类似循环凭借多个字符串这种建议使用 join()函数
package main
import (
"fmt"
"strings"
)
func main() {
var a = []string{`hello`, `你好`,"123"}
fmt.Printf("%s", strings.Join(a, "||"))
}
1.2.6.1strings方法
判断字符开头和结尾
package main
import (
"fmt"
"strings"
)
func main() {
var a string= "kobe is a great basketball player"
fmt.Printf("%t\n" , strings.HasPrefix(a,"kobe"))
fmt.Printf("%t\n" , strings.HasSuffix(a,"jordan"))
}
判断字符包含
var a string= "kobe is a great basketball player"
fmt.Printf("%t\n" , strings.Contains(a,"great"))
fmt.Printf("%t\n" , strings.Contains(a,"bad"))
子串第一次出现的位置
var a string= "kobe is a great basketball player"
fmt.Printf("%d\n" , strings.Index(a,"great"))
子串最后一次出现的位置
fmt.Printf("%d\n" , strings.LastIndex(a,"t"))
字符串替换
(替换前两个?!为@)
var a string= "kobe is a great ?! basketball player ?!"
fmt.Printf("%s\n" , strings.Replace(a,"?!","@",2))
统计字符出现次数
var a string= "kobe is a great ?! basketball player ?!"
fmt.Printf("%d\n" , strings.Count(a,"?!"))
重复字符串
fmt.Printf("%s\n" , strings.Repeat("kobe",2))
大小写转换
fmt.Printf("%s\n" , strings.ToUpper("kobe"))
fmt.Printf("%s\n" , strings.ToLower("JORDAN"))
修剪字符串
//去除开头和结尾的指定字符
fmt.Printf("%s\n" , strings.Trim("??kobe is the best basketball player in the world?","?"))
//剔除开头和结尾的所有空格
fmt.Printf("%s\n" , strings.TrimSpace(" kobe is the best basketball player in the world "))
//只剪开头
fmt.Printf("%s\n" , strings.TrimLeft("??kobe is the best basketball player in the world?","?"))
//只剪结尾
fmt.Printf("%s\n" , strings.TrimRight("??kobe is the best basketball player in the world?","?"))
拆分字符串
按照空白字符拆分字符串,成字符串数组
str := "The quick brown fox jumps over the lazy dog"
sl := strings.Fields(str)
fmt.Printf("Splitted in slice: %v\n", sl)
for _, val := range sl {
fmt.Printf("%s -",val)
}
按照指定字符串切分
str := "kobe|is|the|great|basketball|player"
sl2 := strings.Split(str, "|")
fmt.Println(sl2)
拼接字符串
相当于把|全部换成;了
str := "kobe|is|the|great|basketball|player"
sl2 := strings.Split(str, "|")
fmt.Println(sl2)
str3 := strings.Join(sl2,";")
fmt.Printf("%s\n", str3)
1.2.6.2strconv方法
字符串相关类型转换
package main
import (
"fmt"
"strconv"
)
func main() {
str := "666"
var an int
var newS string
//获取当前操作系统int所占位数
fmt.Printf("the size of int is: %d\n", strconv.IntSize)
//字符串转int
an, _ = strconv.Atoi(str)
fmt.Printf("the integer is : %d\n", an)
an = an + 5
//返回数字表示的字符串类型十进制数
newS = strconv.Itoa(an)
fmt.Printf("the new string is : %s\n", newS)
}
//获取当前时间cst
var t time.Time = time.Now()
fmt.Println(t)
//抽取当前时间的天、月、年等
fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())
//获取当前utc时间
a := time.Now().UTC()
fmt.Println(a)
//time辅助类增加时间,例子是往后推一周
var week time.Duration
week = 60 * 60 * 24 * 7 * 1e9
weekFromNow := a.Add(week)
fmt.Println(weekFromNow)
// 各种时间显示格式:
fmt.Println(t.Format(time.RFC822)) // 21 Dec 11 0852 UTC
fmt.Println(t.Format(time.ANSIC)) // Wed Dec 21 08:56:34 2011
fmt.Println(t.Format("02 Jan 2006 15:04")) // 21 Dec 2011 08:52
s := t.Format("20060102")
fmt.Println(t, "=>", s)
// Wed Dec 21 08:52:14 +0000 UTC 2011 => 20111221
指针类型存储的是内存地址
package main
import (
"fmt"
)
func main() {
var i = 5
fmt.Printf("%d------%p\n", i, &i)
var into *int
into = &i
fmt.Printf("%p----%d\n", into, *into)
}
通过指针也可修改此内存地址上的值
func main() {
s := "good bye"
var p *string = &s
*p = "cao"
fmt.Printf("p--------:%p\n", p) //地址
fmt.Printf("*p-------:%s\n", *p) //cao
fmt.Printf("s--------:%s\n", s) //cao
}
不能得到常量或者文字的地址,这样都是不合法的
func main() {
const s = "good bye"
var p *int = &10
ptr := &s
}
if、if...else
func Abs(x int) int {
if x > 0 {
return x
}else {
return -x
}
}
switch case:
这个不像java,不用break,只执行一行不会继续往下
package main
import "fmt"
func main() {
var num1 int = 98
switch num1 {
case 98, 99:
fmt.Println("from to : 98")
case 100:
fmt.Println("to :100")
default:
fmt.Println("not from 98 to 100")
}
}
package main
import (
"fmt"
"strconv"
)
func main() {
var orig string = "ABC"
// var an int
var newS string
// var err error
fmt.Printf("The size of ints is: %d\n", strconv.IntSize)
// anInt, err = strconv.Atoi(origStr)
an, err := strconv.Atoi(orig)
if err != nil {
fmt.Printf("orig %s is not an integer - exiting with error\n", orig)
return
}
fmt.Printf("The integer is %d\n", an)
an = an + 5
newS = strconv.Itoa(an)
fmt.Printf("%s", newS)
}
一般出错终止喜欢用os.Exit
if err != nil {
fmt.Printf("orig %s is not an integer - exiting with error\n", orig)
os.Exit(1)
}
和java基本一样没啥好说的
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
如下:输出字符串的每个字符以及下标数
package main
import "fmt"
func main() {
str := "Go is a beautiful language!"
fmt.Printf("The length of str is: %d\n", len(str))
for index, char := range str {
fmt.Printf("Character on position %d is: %c \n", index, char)
}
fmt.Println()
}