慕课网Go——1.go语言基础

3. 变量

3.1 定义变量

package main

import "fmt"

// 全局变量可以不使用
var flg bool

func main() {
	//局部变量定义后必须使用,默认零值
	var name int = 1
	fmt.Println(name)
	age := 1
	fmt.Println(age)

	//多变量定义,可以不同类型
	var s1, s2 = "s1", 2
	//可以更改赋值,但不能换类型
	s1 = "s2"
	fmt.Println(s1, s2)

	var (
		s3 = "s3"
		s4 int = 4
		s5 bool
	)
	fmt.Println(s3, s4, s5)
}

3.2 常量

package main

import "fmt"

func main() {
	const PI float32 = 3.14

	const (
		x int = 16
		y//y默认为16
		s = "abc"
		z//z默认为abc
	)
	fmt.Println(x, y, s, z)
}

3.3 iota

package main

import "fmt"

func main() {
	//iota自增
	const (
		err1 = iota
		err2 = iota
		err3 = iota
	)
	fmt.Println(err1, err2, err3) //0 1 2

	const (
		e1 = iota
		e2
		e3 = 100
		e4
		e5 = iota
		e6
	)
	fmt.Println(e1, e2, e3, e4, e5, e6) //0 1 100 100 4 5
}

3.4 匿名变量

package main

import "fmt"

func a() (int, bool) {
	return 0, false
}

func main() {
	var _ int

	r1, _ := a()
	fmt.Println(r1) //0
}

4. 数据类型

4.3 类型转换

package main

import (
	"fmt"
	"strconv"
)

func main() {
	//str->int
	var strint = "12"
	myint, err := strconv.Atoi(strint)
	if err != nil {
		fmt.Println("convert error")
	}
	fmt.Println(myint)
	fmt.Println(strconv.Atoi(strint)) //12 

	//int->str
	var intstr = 32
	fmt.Println(strconv.Itoa(intstr)) //32

	//str->float64
	myfloat, err := strconv.ParseFloat("3.14", 64)
	if err != nil {
		fmt.Println("convert error")
	}
	fmt.Println(myfloat) //-3.14

	//str->int(10进制)
	myint2, err := strconv.ParseInt("-42", 10, 64)
	if err != nil {
		fmt.Println("convert error")
	}
	fmt.Println(myint2) //-42

	//str->bool,0或"false"为false,1或"true"为true,其他数值报错
	mybool, err := strconv.ParseBool("0")
	if err != nil {
		fmt.Println("convert error")
	}
	fmt.Println(mybool) //false
	
	boolstr := strconv.FormatBool(true)
	fmt.Println(boolstr) //true

	floatstr := strconv.FormatFloat(3.1415, 'E', -1, 64)
	fmt.Println(floatstr) //3.1415E+00

	fmt.Println(strconv.FormatInt(-42, 16))//-2a
}

5. 字符串操作

5.4 输出

package main

import (
	"fmt"
	"strings"
)

func main() {
	name := "neo"

	msg := fmt.Sprintf("用户:%s", name)
	fmt.Println(msg)

	var builder strings.Builder
	builder.WriteString("你好")
	builder.WriteString(name)

	re := builder.String()
	fmt.Println(re)
}

5.6 字符串函数

package main

import (
	"fmt"
	"strings"
)

func main() {
	name := ",.hello,go."
	fmt.Println(strings.Contains(name, "go"))
	fmt.Println(strings.HasPrefix(name, "hello"))
	fmt.Println(strings.HasSuffix(name, "go"))

	fmt.Println(strings.Count(name, "o"))

	fmt.Println(strings.Index(name, "he"))

	fmt.Println(strings.Split(name, ","))

	fmt.Println(strings.Trim(name, ",."))//只剪除两边

	fmt.Println(strings.Replace(name, "go", "java", 2))
}

6. 条件控制

package main

import "fmt"

func main() {
	for i := 0; i < 3; i++ {
		fmt.Println(i)
	}

	name := "immocgo"
	for index, value := range name {
		fmt.Println(index, value)
	}

	age := 16
	if age < 18 {
		fmt.Println("child")
	} else if age == 18 {
		fmt.Println("18")
	} else {
		fmt.Println("adult")
	}

	switch age {
	case 18:
		fmt.Println("18")
	case 17, 16:
		fmt.Println("17,16")
	default:
		fmt.Println("default")
	}

	switch {
	case age == 18:
		fmt.Println("18")
	case age < 18:
		fmt.Println("child")
	default:
		fmt.Println("default")
	}
}

你可能感兴趣的:(慕课网Go,golang,开发语言,后端)