提交 f47e7e95 编写于 作者: _Fighter's avatar _Fighter

2021年12月21日

上级 d585c86a
......@@ -19,11 +19,26 @@
<select />
</component>
<component name="ChangeListManager">
<list default="true" id="d365b3bd-6b35-4929-97b9-bcb786acfffc" name="Default Changelist" comment="2021年12月7日">
<change afterPath="$PROJECT_DIR$/nonrepeating.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/strings.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/tree/MenuNode.go" afterDir="false" />
<list default="true" id="d365b3bd-6b35-4929-97b9-bcb786acfffc" name="Default Changelist" comment="2021年12月13日">
<change afterPath="$PROJECT_DIR$/queue/entry/main.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/queue/queue.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/readerWriter/main.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/retriever/main.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/retriever/mock/mockretriever.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/retriever/real/realretriever.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/arrays.go" beforeDir="false" afterPath="$PROJECT_DIR$/arrays/arrays.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/basic.go" beforeDir="false" afterPath="$PROJECT_DIR$/basic/basic.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/branch.go" beforeDir="false" afterPath="$PROJECT_DIR$/branch/branch.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/func.go" beforeDir="false" afterPath="$PROJECT_DIR$/func/func.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/helloworld.go" beforeDir="false" afterPath="$PROJECT_DIR$/helloworld/helloworld.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/nonrepeating.go" beforeDir="false" afterPath="$PROJECT_DIR$/nonrepeating/nonrepeating.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/sliceops.go" beforeDir="false" afterPath="$PROJECT_DIR$/sliceops/sliceops.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/slices.go" beforeDir="false" afterPath="$PROJECT_DIR$/slices/slices.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/strings.go" beforeDir="false" afterPath="$PROJECT_DIR$/string/strings.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test.txt" beforeDir="false" afterPath="$PROJECT_DIR$/test.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tree/MenuNode.go" beforeDir="false" afterPath="$PROJECT_DIR$/tree/MenuNode.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tree/entry/entry.go" beforeDir="false" afterPath="$PROJECT_DIR$/tree/entry/entry.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -33,6 +48,7 @@
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Go File" />
<option value="Go Application" />
</list>
</option>
......@@ -58,74 +74,85 @@
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="go.import.settings.migrated" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_package_manager_path" value="npm" />
</component>
<component name="RunManager" selected="Go Build.go build learngo/tree">
<configuration default="true" type="ArquillianTestNG" factoryName="" nameIsGenerated="true">
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/string" />
<recent name="$PROJECT_DIR$/slices" />
<recent name="$PROJECT_DIR$/sliceops" />
<recent name="$PROJECT_DIR$/nonrepeating" />
<recent name="$PROJECT_DIR$/helloworld" />
</key>
</component>
<component name="RunManager" selected="Go Build.go build learngo/readerWriter">
<configuration default="true" type="ArquillianJUnit" factoryName="" nameIsGenerated="true">
<option name="arquillianRunConfiguration">
<value>
<option name="containerStateName" value="" />
</value>
</option>
<option name="TEST_OBJECT" value="CLASS" />
<properties />
<listeners />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="go build learngo (3)" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<configuration name="go build learngo/queue/entry" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="learngo" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/sliceops.go" />
<package value="learngo" />
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/queue/entry/main.go" />
<package value="learngo/queue/entry" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build learngo (4)" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<configuration name="go build learngo/readerWriter" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="learngo" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/nonrepeating.go" />
<package value="learngo" />
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/readerWriter/main.go" />
<package value="learngo/readerWriter" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build learngo/tree" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<configuration name="go build learngo/retriever" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="learngo" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/tree/MenuNode.go" />
<package value="learngo/tree" />
<filePath value="$PROJECT_DIR$/retriever/main.go" />
<package value="learngo/retriever" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build strings.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<configuration name="go build learngo/tree" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="learngo" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/strings.go" />
<package value="learngo" />
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/tree/MenuNode.go" />
<package value="learngo/tree" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="slices" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true">
<configuration name="go build learngo/tree/entry" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="learngo" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/slices.go" />
<package value="learngo" />
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/tree/entry/entry.go" />
<package value="learngo/tree/entry" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Go Build.go build learngo/readerWriter" />
<item itemvalue="Go Build.go build learngo/retriever" />
<item itemvalue="Go Build.go build learngo/tree/entry" />
<item itemvalue="Go Build.go build learngo/queue/entry" />
<item itemvalue="Go Build.go build learngo/tree" />
<item itemvalue="Go Build.go build learngo (4)" />
<item itemvalue="Go Build.go build strings.go" />
<item itemvalue="Go Build.go build learngo (3)" />
<item itemvalue="Go Build.slices" />
</list>
</recent_temporary>
</component>
......@@ -157,7 +184,17 @@
<workItem from="1639317342734" duration="1661000" />
<workItem from="1639319123956" duration="1167000" />
<workItem from="1639321737905" duration="1332000" />
<workItem from="1639324378299" duration="1154000" />
<workItem from="1639324378299" duration="1611000" />
<workItem from="1639410861527" duration="1813000" />
<workItem from="1639496207161" duration="1452000" />
<workItem from="1639498040381" duration="1603000" />
<workItem from="1639583195649" duration="316000" />
<workItem from="1639585026841" duration="47000" />
<workItem from="1639923387642" duration="581000" />
<workItem from="1640010328393" duration="1512000" />
<workItem from="1640012110718" duration="1690000" />
<workItem from="1640013935368" duration="446000" />
<workItem from="1640101892048" duration="1269000" />
</task>
<task id="LOCAL-00001" summary="2021年12月5日">
<created>1638720681912</created>
......@@ -194,7 +231,49 @@
<option name="project" value="LOCAL" />
<updated>1638894518419</updated>
</task>
<option name="localTasksCounter" value="6" />
<task id="LOCAL-00006" summary="2021年12月10日">
<created>1639325723378</created>
<option name="number" value="00006" />
<option name="presentableId" value="LOCAL-00006" />
<option name="project" value="LOCAL" />
<updated>1639325723379</updated>
</task>
<task id="LOCAL-00007" summary="2021年12月10日">
<created>1639325745611</created>
<option name="number" value="00007" />
<option name="presentableId" value="LOCAL-00007" />
<option name="project" value="LOCAL" />
<updated>1639325745611</updated>
</task>
<task id="LOCAL-00008" summary="2021年12月11日">
<created>1639325758148</created>
<option name="number" value="00008" />
<option name="presentableId" value="LOCAL-00008" />
<option name="project" value="LOCAL" />
<updated>1639325758148</updated>
</task>
<task id="LOCAL-00009" summary="2021年12月11日">
<created>1639325805904</created>
<option name="number" value="00009" />
<option name="presentableId" value="LOCAL-00009" />
<option name="project" value="LOCAL" />
<updated>1639325805904</updated>
</task>
<task id="LOCAL-00010" summary="2021年12月13日">
<created>1639412304915</created>
<option name="number" value="00010" />
<option name="presentableId" value="LOCAL-00010" />
<option name="project" value="LOCAL" />
<updated>1639412304915</updated>
</task>
<task id="LOCAL-00011" summary="2021年12月13日">
<created>1639412316956</created>
<option name="number" value="00011" />
<option name="presentableId" value="LOCAL-00011" />
<option name="project" value="LOCAL" />
<updated>1639412316956</updated>
</task>
<option name="localTasksCounter" value="12" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
......@@ -216,7 +295,10 @@
<MESSAGE value="2021年12月5日" />
<MESSAGE value="2021年12月6日" />
<MESSAGE value="2021年12月7日" />
<option name="LAST_COMMIT_MESSAGE" value="2021年12月7日" />
<MESSAGE value="2021年12月10日" />
<MESSAGE value="2021年12月11日" />
<MESSAGE value="2021年12月13日" />
<option name="LAST_COMMIT_MESSAGE" value="2021年12月13日" />
</component>
<component name="VgoProject">
<integration-enabled>true</integration-enabled>
......@@ -236,30 +318,42 @@
<screen x="-1920" y="-196" width="1920" height="1055" />
</state>
<state x="-1496" y="-10" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.-196.1920.1055/0.25.1311.743@-1920.-196.1920.1055" timestamp="1638463705929" />
<state width="1324" height="297" key="GridCell.Tab.0.bottom" timestamp="1639325577505">
<screen x="0" y="0" width="1366" height="768" />
<state x="496" y="116" key="#com.intellij.ide.util.MemberChooser" timestamp="1640013650164">
<screen x="0" y="25" width="1366" height="743" />
</state>
<state x="496" y="116" key="#com.intellij.ide.util.MemberChooser/1366.-158.1860.1055/0.25.1366.743@0.25.1366.743" timestamp="1640013650164" />
<state width="1324" height="197" key="GridCell.Tab.0.bottom" timestamp="1640103022679">
<screen x="0" y="25" width="1366" height="743" />
</state>
<state width="1878" height="455" key="GridCell.Tab.0.bottom/-1920.-196.1920.1055/0.25.1311.743@-1920.-196.1920.1055" timestamp="1638463841375" />
<state width="1324" height="197" key="GridCell.Tab.0.bottom/1366.-158.1859.1055/0.25.1366.743@0.25.1366.743" timestamp="1640103022679" />
<state width="1817" height="165" key="GridCell.Tab.0.bottom/1366.-158.1859.1055/0.25.1366.743@1366.-158.1859.1055" timestamp="1638894523546" />
<state width="1324" height="297" key="GridCell.Tab.0.bottom/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639325577505" />
<state width="1324" height="297" key="GridCell.Tab.0.center" timestamp="1639325577501">
<screen x="0" y="0" width="1366" height="768" />
<state width="1324" height="199" key="GridCell.Tab.0.bottom/1366.-158.1860.1055/0.25.1366.743@0.25.1366.743" timestamp="1640014417559" />
<state width="1324" height="271" key="GridCell.Tab.0.bottom/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639499455426" />
<state width="1324" height="197" key="GridCell.Tab.0.center" timestamp="1640103022677">
<screen x="0" y="25" width="1366" height="743" />
</state>
<state width="1878" height="455" key="GridCell.Tab.0.center/-1920.-196.1920.1055/0.25.1311.743@-1920.-196.1920.1055" timestamp="1638463841373" />
<state width="1324" height="197" key="GridCell.Tab.0.center/1366.-158.1859.1055/0.25.1366.743@0.25.1366.743" timestamp="1640103022677" />
<state width="1817" height="165" key="GridCell.Tab.0.center/1366.-158.1859.1055/0.25.1366.743@1366.-158.1859.1055" timestamp="1638894523545" />
<state width="1324" height="297" key="GridCell.Tab.0.center/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639325577501" />
<state width="1324" height="297" key="GridCell.Tab.0.left" timestamp="1639325577498">
<screen x="0" y="0" width="1366" height="768" />
<state width="1324" height="199" key="GridCell.Tab.0.center/1366.-158.1860.1055/0.25.1366.743@0.25.1366.743" timestamp="1640014417558" />
<state width="1324" height="271" key="GridCell.Tab.0.center/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639499455424" />
<state width="1324" height="197" key="GridCell.Tab.0.left" timestamp="1640103022676">
<screen x="0" y="25" width="1366" height="743" />
</state>
<state width="1878" height="455" key="GridCell.Tab.0.left/-1920.-196.1920.1055/0.25.1311.743@-1920.-196.1920.1055" timestamp="1638463841373" />
<state width="1324" height="197" key="GridCell.Tab.0.left/1366.-158.1859.1055/0.25.1366.743@0.25.1366.743" timestamp="1640103022676" />
<state width="1817" height="165" key="GridCell.Tab.0.left/1366.-158.1859.1055/0.25.1366.743@1366.-158.1859.1055" timestamp="1638894523545" />
<state width="1324" height="297" key="GridCell.Tab.0.left/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639325577498" />
<state width="1324" height="297" key="GridCell.Tab.0.right" timestamp="1639325577503">
<screen x="0" y="0" width="1366" height="768" />
<state width="1324" height="199" key="GridCell.Tab.0.left/1366.-158.1860.1055/0.25.1366.743@0.25.1366.743" timestamp="1640014417557" />
<state width="1324" height="271" key="GridCell.Tab.0.left/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639499455421" />
<state width="1324" height="197" key="GridCell.Tab.0.right" timestamp="1640103022678">
<screen x="0" y="25" width="1366" height="743" />
</state>
<state width="1878" height="455" key="GridCell.Tab.0.right/-1920.-196.1920.1055/0.25.1311.743@-1920.-196.1920.1055" timestamp="1638463841374" />
<state width="1324" height="197" key="GridCell.Tab.0.right/1366.-158.1859.1055/0.25.1366.743@0.25.1366.743" timestamp="1640103022678" />
<state width="1817" height="165" key="GridCell.Tab.0.right/1366.-158.1859.1055/0.25.1366.743@1366.-158.1859.1055" timestamp="1638894523545" />
<state width="1324" height="297" key="GridCell.Tab.0.right/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639325577503" />
<state width="1324" height="199" key="GridCell.Tab.0.right/1366.-158.1860.1055/0.25.1366.743@0.25.1366.743" timestamp="1640014417558" />
<state width="1324" height="271" key="GridCell.Tab.0.right/1366.-183.1920.1080/0.0.1366.768@0.0.1366.768" timestamp="1639499455425" />
<state width="1324" height="156" key="GridCell.Tab.1.bottom" timestamp="1639319595271">
<screen x="0" y="0" width="1366" height="768" />
</state>
......
......@@ -7,94 +7,97 @@ import (
)
/*此处不能使用 := 来定义变量
go 语言都中包内变量,go 没有全局变量
函数包变量可以不使用,
go 语言都中包内变量,go 没有全局变量
函数包变量可以不使用,
*/
var xx =3
var ss =100
var xx = 3
var ss = 100
/*统一定义变量*/
var(
x1=23
x2=true
x3=323.23
var (
x1 = 23
x2 = true
x3 = 323.23
)
func variableZeroValue() {
func variableZeroValue() {
/*函数内部变量必须被使用*/
var a int
var s string
fmt.Printf("%d %q\n", a, s)
}
func variableInitialValue() {
var a ,b int = 3,4
func variableInitialValue() {
var a, b int = 3, 4
var s string = "abc"
fmt.Println(a,b, s)
fmt.Println(a, b, s)
}
func variableTypeDeduction() {
var x ,b ,c,e,f = 3.23,4,"abdad",true, 4.54
fmt.Println(x,b,f+1, c,e)
func variableTypeDeduction() {
var x, b, c, e, f = 3.23, 4, "abdad", true, 4.54
fmt.Println(x, b, f+1, c, e)
}
func variableShorter() {
/*
func variableShorter() {
/*
不使用 var
第一次定义变量可以使用:=,但是只能在函数内部使用,外部不能使用
*/
x ,b ,c,e,f := 3.23,4,"abdad",true, 4.54
fmt.Println(x,b,f+1, c,e)
*/
x, b, c, e, f := 3.23, 4, "abdad", true, 4.54
fmt.Println(x, b, f+1, c, e)
}
func euler() {
c:=3 +4i
func euler() {
c := 3 + 4i
fmt.Println(cmplx.Abs(c))
}
/*强制转换*/
func triangle() {
a ,b :=3,5
func triangle() {
a, b := 3, 5
var c int
c = int(math.Sqrt(float64(a*a + b*b)))
fmt.Println(c)
}
/*常量*/
func consts() {
func consts() {
/*const filename="abc.txt"
const a, b = 3, 4 */
/* 集中写常量*/
const(
filename ="abc.txt"
a, b = 3, 4
const (
filename = "abc.txt"
a, b = 3, 4
)
var c int
c = int(math.Sqrt(a*a + b*b))
fmt.Println(filename,c )
fmt.Println(filename, c)
}
/*枚举*/
func enums() {
func enums() {
/*常量定义必须给赋值*/
const(
cpp=iota // 自动填充递长
const (
cpp = iota // 自动填充递长
java
python
golang
javasscipt
_ // 下划线跳过自动填充值
_ // 下划线跳过自动填充值
php
)
const x int =0
fmt.Println(x, cpp,python,golang,javasscipt,java,php)
const x int = 0
fmt.Println(x, cpp, python, golang, javasscipt, java, php)
/*设置自增表达式*/
const(
b = 1 <<(10*iota)
const (
b = 1 << (10 * iota)
kb
mb
gb
tb
pb
)
fmt.Println(b,kb,mb,gb,tb,pb)
fmt.Println(b, kb, mb, gb, tb, pb)
}
func main() {
......@@ -102,7 +105,7 @@ func main() {
variableInitialValue()
variableTypeDeduction()
variableShorter()
fmt.Println(x1,x2,x3)
fmt.Println(x1, x2, x3)
euler()
triangle()
consts()
......
package main
import (
"fmt"
"io/ioutil"
)
func ifcondition() {
const filename = "test.txt"
// ReadFile reads the file named by filename and returns the contents.
// A successful call returns err == nil, not err == EOF. Because ReadFile
// reads the whole file, it does not treat an EOF from Read as an error
// to be reported.
contents ,err := ioutil.ReadFile(filename)
// if不需要 括号
if err != nil{
fmt.Println(err)
}else{
fmt.Printf("%s\n" ,contents)
}
// 简单写法
if content ,err := ioutil.ReadFile(filename); err != nil{
fmt.Println(err)
} else{
fmt.Printf("%s\n" ,content)
}
//fmt.Printf("%s\n" ,content) 此处不能再访问 content ,生命周期在if中,外部不能访问
}
func grade(score int ) string {
g:= ""
// switch 后面可以跟 选择参数,也可以不跟参数
switch {
case score < 0 || score >100:
// panic 报错并中断程序执行,
panic(fmt.Sprintf( "Wront score: %d",score))
case score < 60 :
g = "不及格"
case score < 80:
g = "一般"
case score < 90:
g = "良好"
case score <= 100:
g = "优秀"
}
return g
}
func main() {
ifcondition()
fmt.Println(
grade(33),
grade(67),
grade(98),
grade(81),
grade(100),
)
}
package main
import (
"fmt"
"io/ioutil"
)
func ifcondition() {
const filename = "test.txt"
// ReadFile reads the file named by filename and returns the contents.
// A successful call returns err == nil, not err == EOF. Because ReadFile
// reads the whole file, it does not treat an EOF from Read as an error
// to be reported.
contents, err := ioutil.ReadFile(filename)
// if不需要 括号
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s\n", contents)
}
// 简单写法
if content, err := ioutil.ReadFile(filename); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s\n", content)
}
//fmt.Printf("%s\n" ,content) 此处不能再访问 content ,生命周期在if中,外部不能访问
}
func grade(score int) string {
g := ""
// switch 后面可以跟 选择参数,也可以不跟参数
switch {
case score < 0 || score > 100:
// panic 报错并中断程序执行,
panic(fmt.Sprintf("Wront score: %d", score))
case score < 60:
g = "不及格"
case score < 80:
g = "一般"
case score < 90:
g = "良好"
case score <= 100:
g = "优秀"
}
return g
}
func main() {
ifcondition()
fmt.Println(
grade(33),
grade(67),
grade(98),
grade(81),
grade(100),
)
}
package main
import (
"fmt"
"learngo/queue"
)
func main() {
q := queue.Queue{1}
q.Push(2)
q.Push(3)
//q.Push(4)
fmt.Println(q.Pop())
fmt.Println(q.Pop())
fmt.Println(q.IsEmpty())
fmt.Println(q.Pop())
fmt.Println(q.IsEmpty())
}
package queue
// 定义别名
type Queue []interface{}
func (q *Queue) Push(v interface{}) {
*q = append(*q, v)
}
func (q *Queue) Pop() interface{} {
head := (*q)[0]
*q = (*q)[1:]
return head
}
func (q *Queue) IsEmpty() bool {
return len(*q) == 0
}
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
)
func printFile(filename string) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
printFileContests(file)
}
func printFileContests(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
printFile("test.txt")
var str string = "dafwef"
s := `test.txt
sdfasdf
wefwef wefwef"wefwefwfe"
`
printFileContests(strings.NewReader(s))
printFileContests(bytes.NewBufferString(str))
}
package main
import (
"fmt"
"learngo/retriever/mock"
"learngo/retriever/real"
"time"
)
type Retriever interface {
Get(url string) string
}
type Poster interface {
Post(url string, form map[string]string) string
}
const url = "http://www.baidu.com"
func download(r Retriever) string {
return r.Get(url)
}
func post(poster Poster) {
poster.Post(url, map[string]string{
"name": "ccmouse",
"course": "golang",
})
}
type RetrieverPoster interface {
Retriever
Poster
}
func session(s RetrieverPoster) string {
s.Post(url, map[string]string{
"contents": " 修改后的数据",
})
return "OK"
//return s.Get(url)
}
func main() {
var r Retriever
r = mock.Retriever{"this is my test interface"}
// 查询接口类型
fmt.Printf("%T %v", r, r)
inspect(r)
//fmt.Println(download(r))
r = &real.Retriever{
UserAgent: "Mozilla/5.0",
TimeOut: time.Minute,
}
inspect(r)
//fmt.Printf("%T %v" ,r,r )
//fmt.Println(download(r))
// type assertion 断言
if retriever, ok := r.(mock.Retriever); ok {
fmt.Println(retriever.Contents)
} else {
fmt.Println(" not a mock Retriever")
}
fmt.Println(" =================")
retriever := mock.Retriever{"this is default value"}
fmt.Println(session(retriever))
fmt.Println(" ============= post ====")
realretriever := &real.Retriever{UserAgent: "this is default Value"}
fmt.Println(session(realretriever))
fmt.Println(realretriever.UserAgent)
}
func inspect(r Retriever) {
fmt.Printf("%T %v", r, r)
switch v := r.(type) {
case mock.Retriever:
fmt.Println("Contents:", v.Contents)
case *real.Retriever:
fmt.Println("UserAgent:", v.UserAgent)
}
}
package mock
type Retriever struct {
Contents string
}
func (r Retriever) Post(url string, form map[string]string) string {
r.Contents = form["contents"]
return "ok"
}
func (r Retriever) Get(url string) string {
return r.Contents
}
package real
import (
"fmt"
"net/http"
"net/http/httputil"
"time"
)
type Retriever struct {
UserAgent string
TimeOut time.Duration
}
func (r *Retriever) String() string {
return fmt.Sprintf(
"contents=%s", r.UserAgent,
)
}
func (r *Retriever) Post(url string, form map[string]string) string {
r.UserAgent = form["contents"]
return "ok"
}
func (r *Retriever) Get(url string) string {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
result, err := httputil.DumpResponse(resp, true)
resp.Body.Close()
if err != nil {
panic(err)
}
return string(result)
}
......@@ -2,5 +2,5 @@
hello world
aaa
234234
4567890
234234234
\ No newline at end of file
......@@ -25,3 +25,8 @@ func (node *TreeMenuNode) SetName(name string) {
}
node.Name = name
}
// 工厂函数定义
func CraeteTreeMenuNode(name string) *TreeMenuNode {
return &TreeMenuNode{Name: name}
}
......@@ -5,6 +5,25 @@ import (
"learngo/tree"
)
/*
包扩展
1、定义别名
2、使用组合
*/
// 组合 TreeMenuNode
type myTreeNode struct {
node *tree.TreeMenuNode
}
func (myNode *myTreeNode) postOrder() {
node := myTreeNode{myNode.node.Child}
node.postOrder()
treeNode := myTreeNode{myNode.node.Perent}
treeNode.postOrder()
myNode.node.Print()
}
func main() {
var menu tree.TreeMenuNode
......@@ -40,4 +59,18 @@ func main() {
menu.Traverse()
org.Traverse()
// 通过工厂获取
root := tree.CraeteTreeMenuNode("测试")
fmt.Println(root.Name)
root.Perent = tree.CraeteTreeMenuNode("1")
root.Child = tree.CraeteTreeMenuNode("2")
root.Perent.Perent = tree.CraeteTreeMenuNode("01")
root.Child.Child = tree.CraeteTreeMenuNode("02")
root.Perent.Print()
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册