Skip to content

Commit

Permalink
学习Go代码—快速入门级别。
Browse files Browse the repository at this point in the history
  • Loading branch information
ling0900 committed Sep 7, 2024
0 parents commit 7cc8aac
Show file tree
Hide file tree
Showing 68 changed files with 3,245 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added 1-firstGolang/hello
Binary file not shown.
19 changes: 19 additions & 0 deletions 1-firstGolang/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main //程序的包名

/*
import "fmt"
import "time"
*/
import (
"fmt"
"time"
)


//main函数
func main() { //函数的{ 一定是 和函数名在同一行的,否则编译错误
//golang中的表达式,加";", 和不加 都可以,建议是不加
fmt.Println(" hello Go!")

time.Sleep(1 * time.Second)
}
44 changes: 44 additions & 0 deletions 10-OOP/test1_struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import "fmt"

//声明一种行的数据类型 myint, 是int的一个别名
type myint int

//定义一个结构体
type Book struct {
title string
auth string
}

func changeBook(book Book) {
//传递一个book的副本
book.auth = "666"
}

func changeBook2(book *Book) {
//指针传递
book.auth = "777"
}

func main() {
/*
var a myint = 10
fmt.Println("a = ", a)
fmt.Printf("type of a = %T\n", a)
*/

var book1 Book
book1.title = "Golang"
book1.auth = "zhang3"

fmt.Printf("%v\n", book1)

changeBook(book1)

fmt.Printf("%v\n", book1)

changeBook2(&book1)

fmt.Printf("%v\n", book1)
}
53 changes: 53 additions & 0 deletions 10-OOP/test2_class.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import "fmt"

//如果类名首字母大写,表示其他包也能够访问
type Hero struct {
//如果说类的属性首字母大写, 表示该属性是对外能够访问的,否则的话只能够类的内部访问
Name string
Ad int
level int
}

/*
func (this Hero) Show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.Level)
}
func (this Hero) GetName() string {
return this.Name
}
func (this Hero) SetName(newName string) {
//this 是调用该方法的对象的一个副本(拷贝)
this.Name = newName
}
*/
func (this *Hero) Show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.level)
}

func (this *Hero) GetName() string {
return this.Name
}

func (this *Hero) SetName(newName string) {
//this 是调用该方法的对象的一个副本(拷贝)
this.Name = newName
}

func main() {
//创建一个对象
hero := Hero{Name: "zhang3", Ad: 100}

hero.Show()

hero.SetName("li4")

hero.Show()
}
60 changes: 60 additions & 0 deletions 10-OOP/test3_class2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import "fmt"

type Human struct {
name string
sex string
}

func (this *Human) Eat() {
fmt.Println("Human.Eat()...")
}

func (this *Human) Walk() {
fmt.Println("Human.Walk()...")
}

//=================

type SuperMan struct {
Human //SuperMan类继承了Human类的方法

level int
}

//重定义父类的方法Eat()
func (this *SuperMan) Eat() {
fmt.Println("SuperMan.Eat()...")
}

//子类的新方法
func (this *SuperMan) Fly() {
fmt.Println("SuperMan.Fly()...")
}

func (this *SuperMan) Print() {
fmt.Println("name = ", this.name)
fmt.Println("sex = ", this.sex)
fmt.Println("level = ", this.level)
}

func main() {
h := Human{"zhang3", "female"}

h.Eat()
h.Walk()

//定义一个子类对象
//s := SuperMan{Human{"li4", "female"}, 88}
var s SuperMan
s.name = "li4"
s.sex = "male"
s.level = 88

s.Walk() //父类的方法
s.Eat() //子类的方法
s.Fly() //子类的方法

s.Print()
}
68 changes: 68 additions & 0 deletions 10-OOP/test4_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import "fmt"

//本质是一个指针
type AnimalIF interface {
Sleep()
GetColor() string //获取动物的颜色
GetType() string //获取动物的种类
}

//具体的类
type Cat struct {
color string //猫的颜色
}

func (this *Cat) Sleep() {
fmt.Println("Cat is Sleep")
}

func (this *Cat) GetColor() string {
return this.color
}

func (this *Cat) GetType() string {
return "Cat"
}

//具体的类
type Dog struct {
color string
}

func (this *Dog) Sleep() {
fmt.Println("Dog is Sleep")
}

func (this *Dog) GetColor() string {
return this.color
}

func (this *Dog) GetType() string {
return "Dog"
}

func showAnimal(animal AnimalIF) {
animal.Sleep() //多态
fmt.Println("color = ", animal.GetColor())
fmt.Println("kind = ", animal.GetType())
}

func main() {

var animal AnimalIF //接口的数据类型, 父类指针
animal = &Cat{"Green"}

animal.Sleep() //调用的就是Cat的Sleep()方法 , 多态的现象

animal = &Dog{"Yellow"}

animal.Sleep() // 调用Dog的Sleep方法,多态的现象

cat := Cat{"Green"}
dog := Dog{"Yellow"}

showAnimal(&cat)
showAnimal(&dog)
}
34 changes: 34 additions & 0 deletions 10-OOP/test5_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import "fmt"

//interface{}是万能数据类型
func myFunc(arg interface{}) {
fmt.Println("myFunc is called...")
fmt.Println(arg)

//interface{} 改如何区分 此时引用的底层数据类型到底是什么?

//给 interface{} 提供 “类型断言” 的机制
value, ok := arg.(string)
if !ok {
fmt.Println("arg is not string type")
} else {
fmt.Println("arg is string type, value = ", value)

fmt.Printf("value type is %T\n", value)
}
}

type Book struct {
auth string
}

func main() {
book := Book{"Golang"}

myFunc(book)
myFunc(100)
myFunc("abc")
myFunc(3.14)
}
17 changes: 17 additions & 0 deletions 11-reflect/test1_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "fmt"

func main() {

var a string
//pair<statictype:string, value:"aceld">
a = "aceld"

//pair<type:string, value:"aceld">
var allType interface{}
allType = a

str, _ := allType.(string)
fmt.Println(str)
}
29 changes: 29 additions & 0 deletions 11-reflect/test2_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"io"
"os"
)

func main() {
//tty: pair<type:*os.File, value:"/dev/tty"文件描述符>
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)

if err != nil {
fmt.Println("open file error", err)
return
}

//r: pair<type: , value:>
var r io.Reader
//r: pair<type:*os.File, value:"/dev/tty"文件描述符>
r = tty

//w: pair<type: , value:>
var w io.Writer
//w: pair<type:*os.File, value:"/dev/tty"文件描述符>
w = r.(io.Writer)

w.Write([]byte("HELLO THIS is A TEST!!!\n"))
}
41 changes: 41 additions & 0 deletions 11-reflect/test3_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import "fmt"

type Reader interface {
ReadBook()
}

type Writer interface {
WriteBook()
}

//具体类型
type Book struct {
}

func (this *Book) ReadBook() {
fmt.Println("Read a Book")
}

func (this *Book) WriteBook() {
fmt.Println("Write a Book")
}

func main() {
//b: pair<type:Book, value:book{}地址>
b := &Book{}

//r: pair<type:, value:>
var r Reader
//r: pair<type:Book, value:book{}地址>
r = b

r.ReadBook()

var w Writer
//r: pair<type:Book, value:book{}地址>
w = r.(Writer) //此处的断言为什么会成功? 因为w r 具体的type是一致

w.WriteBook()
}
17 changes: 17 additions & 0 deletions 11-reflect/test4_reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"reflect"
)

func reflectNum(arg interface{}) {
fmt.Println("type : ", reflect.TypeOf(arg))
fmt.Println("value : ", reflect.ValueOf(arg))
}

func main() {
var num float64 = 1.2345

reflectNum(num)
}
Loading

0 comments on commit 7cc8aac

Please sign in to comment.