Go语言学习笔记(一)
基本介绍
基本语法
Hello,world
package main
import (
"fmt"
)
func main() {
fmt.Print("htllo,woeld")
}
变量
package main
import "fmt"
func main() {
var a int = 10;
var b="string"
var c float64
c=3.6;
f:="3434"
g:=float64(36.2);
fmt.Println(g)
fmt.Println(a,b,c,f)
}
if else
package main
import "fmt"
func main() {
n := 9
if n > 10 {
fmt.Print("n>10")
} else if n>8 {
fmt.Print("测试")
} else {
fmt.Println("n<10")
}
}
for
package main
import "fmt"
func main() {
for i:=0;i<10;i++ {
fmt.Println(i)
}
}
switc
package main
import (
"fmt"
"time"
)
func main() {
a:="string1"
switch a {
case "string":
fmt.Print("string")
case "e":
fmt.Println("e")
default:
fmt.Println("默认输出")
}
now := time.Now()
switch {
case now.Hour()>10:
fmt.Print("大于十点")
default:
fmt.Println("小于十点")
}
}
数组
package main
import "fmt"
func main() {
var a[10] int;
a[1]=0;
var b=[]int{1,2,3,4,5};
fmt.Print(b)
}
切片
package main
import "fmt"
func main() {
var s=make([]int,5);
for i:=0;i<5;i++ {
s[i]=i;
}
fmt.Print(s)
s= append(s, 5)
s = append(s, 10)
fmt.Println(s)
fmt.Println(s[2:5])
fmt.Println(s[:5])
}
map
package main
import "fmt"
func main() {
var m=make(map[string]int)
m["lmx"]=1;
m["zhangsan"]=2;
r,ok:=m["wanwu"]
fmt.Println(m,r,ok)
}
range 函数
package main
import "fmt"
func main() {
var a = []int{1, 2, 5, 6}
var b = map[string]int{"one": 1, "tow": 2}
a = append(a, 10)
for i, k := range a {
fmt.Println(i, k)
}
for k, v := range b {
fmt.Println(k, v)
}
fmt.Println(a)
}
函数
package main
func add(a,b int) int {
return a+b;
}
func mul(a int,b int) int {
return a*b;
}
func exesit(s map[string]string,key string) (v string,ok bool) {
r,ok := s[key]
return r,ok
}
func main() {
println(add(2, 3))
println(mul(2, 3))
v, ok := exesit(map[string]string{"12": "34"}, "2")
println(v,ok)
}
指针
package main
import "fmt"
func addmy(a *int) {
*a++;
}
func main() {
var s =10;
addmy(&s)
fmt.Println(s)
}
结构体
package main
import "fmt"
type user struct {
name string
password string
}
func main() {
s:=user{"lmx","343434"}
t:=user{password: "3434"};
fmt.Println(s)
fmt.Println(t)
}
结构体方法
package main
import "fmt"
type user struct {
name string
password string
}
func (u user) checkPassword() bool {
if u.password == "1212" {
return true
} else {
return false
}
}
func (u *user) setpassword(password string) {
u.password=password
}
func main() {
s := user{"lmx", "343434"}
fmt.Println(s)
fmt.Println(s.checkPassword())
s.setpassword("2323")
fmt.Println(s)
}
异常处理
package main
import (
"errors"
"fmt"
)
func adderror(a,b int) (sum int,err error) {
if a < b {
return a+b,nil
}else {
return 0, errors.New("a必须小于b")
}
}
func main() {
i,err := adderror(56, 3)
if err!=nil {
fmt.Println(err)
return
}
fmt.Println(i)
}
字符串方法
格式化方法
json
package main
import (
"encoding/json"
"fmt"
)
type user struct {
Age int `json:"age"`
Address string
Foods []string
}
func main() {
u:=user{15,"山西大学",[]string{"苹果","烟"}}
marshal, err := json.Marshal(u)
fmt.Println(marshal)
if err!= nil {
panic(err)
}
fmt.Println(string(marshal))
}
时间处理
数字处理
package main
import (
"fmt"
"strconv"
)
func main() {
parseInt, _ := strconv.ParseInt("1111", 10, 64)
float, _ := strconv.ParseFloat("111.4545", 64)
atoi, _ := strconv.Atoi("2323")
fmt.Println(parseInt,float,atoi)
}
进程信息
猜数字游戏
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
num := rand.Intn(100)
for true {
reader := bufio.NewReader(os.Stdin)
readString, err := reader.ReadString('\n')
if err != nil {
fmt.Println("输入不合法,请重新输入")
continue
}
readString = strings.TrimSuffix(readString, "\n")
parseInt, err := strconv.Atoi(readString)
if err != nil {
fmt.Println("输入不合法,请重新输入")
continue
}
if parseInt < num {
fmt.Println("小了")
} else if parseInt > num {
fmt.Println("大了")
} else {
fmt.Println("恭喜你,猜对了")
break
}
}
}