合并最新的学习代码
This commit is contained in:
parent
19de2f187d
commit
e715d9278a
4
.idea/go.iml
Normal file
4
.idea/go.iml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
</module>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
72
.idea/workspace.xml
Normal file
72
.idea/workspace.xml
Normal file
@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AutoImportSettings">
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="495445f0-1eca-47b4-946d-d66cf898be6c" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="FileTemplateManagerImpl">
|
||||
<option name="RECENT_TEMPLATES">
|
||||
<list>
|
||||
<option value="Go File" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="GOROOT" url="file:///usr/local/go" />
|
||||
<component name="ProjectColorInfo">{
|
||||
"associatedIndex": 7
|
||||
}</component>
|
||||
<component name="ProjectId" id="2lF3CQOFOFWwbyYijFatmfemAXC" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"DefaultGoTemplateProperty": "Go File",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
||||
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||
"go.import.settings.migrated": "true",
|
||||
"go.sdk.automatically.set": "true",
|
||||
"last_opened_file_path": "/Users/chenqiang/workspace/study/go",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"settings.editor.selected.configurable": "preferences.pluginManager"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="SharedIndexes">
|
||||
<attachedChunks>
|
||||
<set>
|
||||
<option value="bundled-gosdk-33c477a475b1-b97fc8a1e17c-org.jetbrains.plugins.go.sharedIndexes.bundled-GO-241.14494.238" />
|
||||
<option value="bundled-js-predefined-1d06a55b98c1-74d2a5396914-JavaScript-GO-241.14494.238" />
|
||||
</set>
|
||||
</attachedChunks>
|
||||
</component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
<option name="version" value="3" />
|
||||
</component>
|
||||
<component name="VgoProject">
|
||||
<settings-migrated>true</settings-migrated>
|
||||
</component>
|
||||
<component name="XDebuggerManager">
|
||||
<breakpoint-manager>
|
||||
<breakpoints>
|
||||
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
|
||||
<url>file://$PROJECT_DIR$/tour/tour.go</url>
|
||||
<line>6</line>
|
||||
<option name="timeStamp" value="1" />
|
||||
</line-breakpoint>
|
||||
</breakpoints>
|
||||
</breakpoint-manager>
|
||||
</component>
|
||||
</project>
|
3
flysnow/ch02/go.mod
Normal file
3
flysnow/ch02/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module flysnow/ch02
|
||||
|
||||
go 1.23.0
|
52
flysnow/ch02/main.go
Normal file
52
flysnow/ch02/main.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var f32 float32 = 2.2
|
||||
var f64 float64 = 10.2345
|
||||
fmt.Println("f32 is", f32, ", f64 is", f64)
|
||||
var bf bool = false
|
||||
var bt bool = true
|
||||
fmt.Println("bf is", bf, ", bt is", bt)
|
||||
var s1 string = "Hello"
|
||||
var s2 string = "世界"
|
||||
fmt.Println("s1 is", s1, ", s2 is", s2)
|
||||
fmt.Println("s1+s2=", s1+s2)
|
||||
// 零值
|
||||
var zi int
|
||||
var zf float64
|
||||
var zb bool
|
||||
var zs string
|
||||
fmt.Println("zi is", zi, ", zf is", zf, "zb is", zb, ", zs is", zs)
|
||||
i := 3.14
|
||||
pi := &i
|
||||
fmt.Println(*pi)
|
||||
|
||||
// 常量iota
|
||||
const (
|
||||
one = iota + 1
|
||||
two
|
||||
three
|
||||
four
|
||||
)
|
||||
fmt.Println(one, two, three, four)
|
||||
|
||||
// string
|
||||
j := 5
|
||||
i2s := strconv.Itoa(j)
|
||||
s2i, err := strconv.Atoi(i2s)
|
||||
fmt.Println(i2s, s2i, err)
|
||||
f2s := strconv.FormatFloat(f64, 'e', -1, 32)
|
||||
fmt.Println(f2s)
|
||||
// 判断字符串开头是否为“H”
|
||||
fmt.Println(strings.HasPrefix(f2s, "H"))
|
||||
// 查找字符串
|
||||
fmt.Println(strings.Index(f2s, "e"))
|
||||
// 全部转大写
|
||||
fmt.Println(strings.ToUpper(f2s))
|
||||
}
|
3
flysnow/ch03/go.mod
Normal file
3
flysnow/ch03/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module flysnow/ch03
|
||||
|
||||
go 1.23.0
|
74
flysnow/ch03/main.go
Normal file
74
flysnow/ch03/main.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
i := 10
|
||||
if i > 10 {
|
||||
fmt.Println("i > 10")
|
||||
} else {
|
||||
fmt.Println("i <= 10")
|
||||
}
|
||||
|
||||
i = 6
|
||||
if i > 10 {
|
||||
fmt.Println("i > 10")
|
||||
} else if i > 5 && i <= 10 {
|
||||
fmt.Println("5 < i <= 10")
|
||||
} else {
|
||||
fmt.Println("i <= 5")
|
||||
}
|
||||
|
||||
switch {
|
||||
case i > 10:
|
||||
fmt.Println("i > 10")
|
||||
case i > 5 && i <= 10:
|
||||
fmt.Println("5 < i <= 10")
|
||||
default:
|
||||
fmt.Println("i <= 5")
|
||||
}
|
||||
|
||||
switch j := 1; j {
|
||||
case 1:
|
||||
fallthrough
|
||||
case 2:
|
||||
fmt.Println(j)
|
||||
default:
|
||||
fmt.Println("没有匹配")
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for i := 1; i <= 100; i++ {
|
||||
sum += i
|
||||
}
|
||||
fmt.Println(sum)
|
||||
|
||||
sum = 0
|
||||
i = 1
|
||||
for i <= 100 {
|
||||
sum += i
|
||||
i++
|
||||
}
|
||||
fmt.Println("the sum is", sum)
|
||||
|
||||
sum = 0
|
||||
i = 1
|
||||
for {
|
||||
sum += i
|
||||
i++
|
||||
if i > 100 {
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println("the sum is", sum)
|
||||
|
||||
sum = 0
|
||||
for i := 0; i < 100; i++ {
|
||||
if i%2 != 0 {
|
||||
continue
|
||||
}
|
||||
sum += i
|
||||
}
|
||||
fmt.Println("the sum is", sum)
|
||||
|
||||
}
|
3
flysnow/ch04/go.mod
Normal file
3
flysnow/ch04/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module flysnow/ch04
|
||||
|
||||
go 1.23.0
|
59
flysnow/ch04/main.go
Normal file
59
flysnow/ch04/main.go
Normal file
@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := []string{"a", "b", "c", "d", "e"}
|
||||
fmt.Println(array)
|
||||
for i := 0; i < len(array); i++ {
|
||||
fmt.Printf("数组索引: %d,对应值: %s\n", i, array[i])
|
||||
}
|
||||
for i, v := range array {
|
||||
fmt.Printf("数组索引: %d, 对应值: %s\n", i, v)
|
||||
}
|
||||
for _, v := range array {
|
||||
fmt.Printf("数组值: %s\n", v)
|
||||
}
|
||||
// 切片slice
|
||||
slice := array[2:4] // 包含索引2,不包含索引4
|
||||
fmt.Println(slice)
|
||||
slice1 := make([]string, 4)
|
||||
slice2 := make([]string, 4, 8)
|
||||
slice2 = append(slice1, "f")
|
||||
fmt.Println(slice2)
|
||||
// 映射map
|
||||
nameAgeMap := make(map[string]int)
|
||||
nameAgeMap["Snowfay"] = 20
|
||||
fmt.Println(nameAgeMap)
|
||||
age := nameAgeMap["Snowfay"]
|
||||
fmt.Println(age)
|
||||
age, ok := nameAgeMap["Snowfay"]
|
||||
if ok {
|
||||
fmt.Println(age)
|
||||
} else {
|
||||
fmt.Println(ok)
|
||||
}
|
||||
nameAgeMap["飞雪无情"] = 50
|
||||
fmt.Println(nameAgeMap)
|
||||
delete(nameAgeMap, "飞雪无情")
|
||||
fmt.Println(nameAgeMap)
|
||||
nameAgeMap["Hello"] = 22
|
||||
nameAgeMap["Allen"] = 33
|
||||
for k, v := range nameAgeMap {
|
||||
fmt.Println("Key is", k, "Value is", v)
|
||||
}
|
||||
fmt.Println(len(nameAgeMap))
|
||||
// string
|
||||
s := "Hello飞雪无情"
|
||||
bs := []byte(s)
|
||||
fmt.Println(bs)
|
||||
fmt.Println(s[0], s[1], s[15])
|
||||
fmt.Println(len(s))
|
||||
fmt.Println(utf8.RuneCountInString(s))
|
||||
for i, r := range s {
|
||||
fmt.Printf("%d %c\n", i, r)
|
||||
}
|
||||
}
|
3
flysnow/ch05/go.mod
Normal file
3
flysnow/ch05/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module flysnow/ch05
|
||||
|
||||
go 1.23.0
|
70
flysnow/ch05/main.go
Normal file
70
flysnow/ch05/main.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Age uint
|
||||
|
||||
func (age Age) String() {
|
||||
fmt.Println("the age is", age)
|
||||
}
|
||||
|
||||
func (age *Age) Modify() {
|
||||
*age = Age(30)
|
||||
}
|
||||
|
||||
func main() {
|
||||
age := Age(25)
|
||||
age.String()
|
||||
age.Modify()
|
||||
age.String()
|
||||
|
||||
sm := Age.String
|
||||
sm(age)
|
||||
|
||||
result, err := sum(-1, 2)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(result)
|
||||
}
|
||||
fmt.Println(sum1(1, 2))
|
||||
fmt.Println(sum1(1, 2, 3))
|
||||
fmt.Println(sum1(1, 2, 3, 4))
|
||||
|
||||
sum2 := func(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
fmt.Println(sum2(1, 2))
|
||||
c1 := colsure()
|
||||
fmt.Println(c1())
|
||||
fmt.Println(c1())
|
||||
fmt.Println(c1())
|
||||
}
|
||||
|
||||
func colsure() func() int {
|
||||
i := 0
|
||||
return func() int {
|
||||
i++
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
func sum(a int, b int) (sum int, err error) {
|
||||
if a < 0 || b < 0 {
|
||||
return 0, errors.New("a或者b不能是负数")
|
||||
}
|
||||
sum = a + b
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
|
||||
func sum1(params ...int) int {
|
||||
sum := 0
|
||||
for _, i := range params {
|
||||
sum += i
|
||||
}
|
||||
return sum
|
||||
}
|
3
flysnow/ch06/go.mod
Normal file
3
flysnow/ch06/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module flysnow/ch06
|
||||
|
||||
go 1.23.0
|
10
flysnow/ch06/main.go
Normal file
10
flysnow/ch06/main.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
type person struct {
|
||||
name string
|
||||
age uint
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
3
flysnow/tour/go.mod
Normal file
3
flysnow/tour/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module flysnow/tour
|
||||
|
||||
go 1.23.0
|
7
flysnow/tour/tour.go
Normal file
7
flysnow/tour/tour.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("hello world")
|
||||
}
|
8
helloworld/.idea/.gitignore
vendored
Normal file
8
helloworld/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
9
helloworld/.idea/go.iml
Normal file
9
helloworld/.idea/go.iml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
helloworld/.idea/modules.xml
Normal file
8
helloworld/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/go.iml" filepath="$PROJECT_DIR$/.idea/go.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
3
helloworld/greetings/go.mod
Normal file
3
helloworld/greetings/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module example.com/greetings
|
||||
|
||||
go 1.16
|
8
helloworld/greetings/greetings.go
Normal file
8
helloworld/greetings/greetings.go
Normal file
@ -0,0 +1,8 @@
|
||||
package greetings
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Hello(name string) string {
|
||||
message := fmt.Sprintf("Hi, %v. Welcome!", name)
|
||||
return message
|
||||
}
|
7
helloworld/hello/go.mod
Normal file
7
helloworld/hello/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module example.com/hello
|
||||
|
||||
go 1.23.0
|
||||
|
||||
replace example.com/greetings => ../greetings
|
||||
|
||||
require example.com/greetings v0.0.0-00010101000000-000000000000
|
11
helloworld/hello/hello.go
Normal file
11
helloworld/hello/hello.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"example.com/greetings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
message := greetings.Hello("Gladys")
|
||||
fmt.Println(message)
|
||||
}
|
Loading…
Reference in New Issue
Block a user