Go语言学习笔记一

2021-11-22 Study 481

1-2节

Go配置国内镜像的命令:

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

1-3节

goland创建go项目,一定要选Go Modules (vgo) 项目

module 后面更的如果是一个git仓库 是一个开源的go项目,该仓库可以不存在,但在引用的时候需要调用该网址。

go语言入口

package main

import "fmt"
func main(){
   fmt.Println("Hello Word!")
}

goland的插件:

File Watchers 格式化go语言代码,添加goimports格式化文件。

2-1节

定义变量:

var a int

变量名写在前面,变量类型写在后面。

var a = 3
var b = 6
var c = true

//var集中定义
var (
   a2 = 3
   b2 = 6
   c2 = true
)

// 空变量
func variableZeroValue() {
   var a int
   var s string
   fmt.Println(a, s)
}

// 赋初始值变量
func variableInitialValue() {
   var a, b int = 3, 5
   var s string = "abc"
   fmt.Println(a, s, b)
}

// 不规定类型的变量
func variableTypeDeduction() {
   var a, b, c, s = 3, 5, true, "哈哈"
   fmt.Println(a, b, c, s)
}

// 省略var 用:=定义变量 函数外面不能用该符号定义变量
func variableShorter() {
   a, b, c, s := 3, 5, true, "哈哈"
   fmt.Println(a, b, c, s)
}

//常量
func consts() {
   const filename = "abc.txt"
   const a, b = 3, 4
}

//枚举类型
func enmus() {
	const (
		java   = 1
		python = 2
		golang = 3
	)
}

//枚举简化
func enmus_simp() {
	const (
		//自增值的
		cpp = iota

		_ //这里可以跳过某个自增值
		java
		python
		golang
	)
}

// b kb mb gb tb gb 设置自增公式
func enmus_iota()  {
	const (
		b = 1<< (10*iota)
		kb
		mb
		gb
		tb
		pb
	)
}

2-4 条件语句

if 语句

func main() {
   const filename = "abc.txt"
   if contents, err := ioutil.ReadFile(filename); err != nil {
      fmt.Println(err)
   } else {
      fmt.Printf("%s\n", contents)
   }
}
func grade(score int) string {
   g := ""
   switch {
   case score < 0 || score > 100:
      panic(fmt.Sprintf("Woring score: %d", score))
   case score < 60:
      g = "F"
   case score < 70:
      g = "C"
   case score < 80:
      g = "B"
   case score <= 100:
      g = "A"
   }
   return g
}

2-5 循环

package main

import "fmt"

func main() {
var arr1 [5]int
arr2 := [3]int{1, 3, 5}
arr3 := [...]int{2, 4, 6, 8, 10}
var grid [4][5]int

fmt.Println(arr1, arr2, arr3, grid)

for i := 0; i < len(arr3); i++ {
fmt.Println(arr3[i])
}

for i := range arr3 {
fmt.Println(arr3[i])
}

for i, v := range arr3 {
fmt.Println(i, v)
}

}

2-6 函数

package main

import "fmt"

func eval(a int, b int, op string) int {
	switch op {
	case "+":
		return a + b
	case "-":
		return a - b
	case "*":
		return a * b
	case "/":
		return a / b
	default:
		panic("unsupported operation: " + op)
	}
}

func main() {
	fmt.Println(eval(3, 4, "*"))
}

2-7 指针

*int 代表指针

指针不能运算

go语言只有值传递

func swap(a, b *int) {
   *b, *a = *a, *b
}

func main() {
   a, b := 3, 4
   swap(&a, &b)
   fmt.Println(a, b)
}

另外一种互换的写法

func swapB(a, b int) (int , int){
   return b,a
}

func main() {
   a, b := 3, 4
   swapB(a, b)
   fmt.Println(a, b)
}

3-1 数组

package main

import "fmt"

func main() {
var arr1 [5]int
arr2 := [3]int{1, 3, 5}
arr3 := [...]int{2, 4, 6, 8, 10}
var grid [4][5]int

fmt.Println(arr1, arr2, arr3, grid)

for i := 0; i < len(arr3); i++ {
fmt.Println(arr3[i])
}

for i := range arr3 {
fmt.Println(arr3[i])
}

for i, v := range arr3 {
fmt.Println(i, v)
}

}

3-2 切片 slice

package main

import "fmt"

func updateSlice(s []int) {
s[0] = 100
}

func main() {
arr1 := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
fmt.Println("arr1[2:6]", arr1[2:6])
fmt.Println("arr1[2:]", arr1[2:])
fmt.Println("arr1[:6]", arr1[:6])
fmt.Println("arr1[:]", arr1[:])
s1 := arr1[2:6]
s2 := arr1[:]
fmt.Println("After updateSlice(s1)")
updateSlice(s1)
fmt.Println(s1)
fmt.Println(arr1)

fmt.Println("After updateSlice(s2)")
updateSlice(s2)
fmt.Println(s2)
fmt.Println(arr1)

s3 := arr1[2:6]
s4 := s3[3:5]
//切片可以扩展的
fmt.Println(s3, s4)
}

3-4 map

map遍历也可以使用 range

遍历没有顺序,如需顺序,需手动对key排序

使用len获取map的个数

package main

import "fmt"

func main() {
m := map[string]string{
"name": "小张不慌张",
"course": "golang",
}

//空map的定义
m2 := make(map[string]int) // m2 == empty map

var m3 map[string]int // m3 == nil
fmt.Println(m, m2, m3)

fmt.Println("map遍历")
// map是无序的 随机出的
for k, v := range m {
fmt.Println(k, v)
}

fmt.Println("map拿值")
courseName := m["course"]
fmt.Println(courseName)
causeName, ok := m["cause"]
fmt.Println(causeName, ok)
name, ok := m["name"]
fmt.Println(name, ok)
delete(m, "name")
name, ok = m["name"]
fmt.Println(name, ok)
}

3-5 map问题

问题:寻找最长不含有重复字符的子串

解释:

abcabcbb -> 答案就是 abc

bbbbb -> 答案是 b

pwwkew -> 答案是 wke

package main

func lengthOfNonRepeatingSubStr(s string) int {
lastOccurred := make(map[byte]int)
start := 0
maxLength := 0
for i, ch := range []byte(s) {
lastI, ok := lastOccurred[ch]
if ok && lastI >= start {
start = lastOccurred[ch] + 1
}
if i-start+1 > maxLength {
maxLength = i - start + 1
}
lastOccurred[ch] = i
}
return maxLength
}

func main() {
lengthOfNonRepeatingSubStr("abcabcbb")
}

3-6 字符和字符串处理

package main

import "fmt"

func main() {
s := "我爱火数云"
for i, ch := range []rune(s) {
fmt.Printf("(%d %c)", i, ch)
}
fmt.Println()
}

4-1 面向对象

go语言仅支持封装,不支持继承和多态

go语言没有class 只有 struct

1 条评论

发布评论

© 2021 All Rights Reserved DO.TO

云服务器自豪的采用火数云