GO 语言学习
安装:
sudo apt-get installgolang-go
export GOROOT=$HOME/go
exportPATH=$PATH:$GOROOT/bin
一,hello world 程序
package main //这个包的名称 import “fmt” //包含 “fmt”,fmt里有输入输出 func main(){ fmt.Printf(“Hello world!\n”); }
注意: main()函数,大括号“{”要跟函数名同一行,如果像c语言那样,编译器会报错的。
编译:
go build a.go
这里的a.go是源文件名
二 使用hash 表
package main import ( "fmt" ) type Data struct{ date string; value int; } func main() { fmt.Printf("hello world!\n"); var m = make(map[string]int); //创建一个空白的hash m["str1"] = 10; m["str2"] = 1000; fmt.Printf("%d\n",m["str1"]); // 遍历 hash for name,value := range m { fmt.Printf("%s\t%d\n",name,value); } }
三 赋值语句特点
googledocument引用:
Multipleassignment
Thelanguage specification has long guaranteed that in assignments theright-hand-side expressions are all evaluated before anyleft-hand-side expressions are assigned. To guarantee predictablebehavior, Go 1 refines the specification further.
Ifthe left-hand side of the assignment statement contains expressionsthat require evaluation, such as function calls or array indexingoperations, these will all be done using the usual left-to-right rulebefore any variables are assigned their value. Once everything isevaluated, the actual assignments proceed in left-to-right order.
Theseexamples illustrate the behavior.
sa := []int{1, 2, 3} i := 0 i, sa[i] = 1, 2 // sets i = 1, sa[0] = 2 sb := []int{1, 2, 3} j := 0 sb[j], j = 2, 1 // sets sb[0] = 2, j = 1 sc := []int{1, 2, 3} sc[0], sc[0] = 1, 2 // sets sc[0] = 1, then sc[0] = 2 (so sc[0] = 2 at end)
Updating:This is one change where tools cannot help, but breakage is unlikely.No code in the standard repository was broken by this change, andcode that depended on the previous unspecified behavior was alreadyincorrect.
四 函数定义与使用
package main import ( "fmt" ) // (x int) 是参数表 // (r int) 是返回结果 func fun(x int)(r int){ if x == 1 { return 1; }else { return fun(x -1 ) * x; } return ; } func main() { fmt.Printf("%d\n",fun(3)); }
五 常量与变量
package main import ( "fmt" "os" ) const pi = 3.14; // 定义一个常变量 const pp = 2/3; func main() { var a int = 10; //定义一个整型变量,并初始化为 10 var goos = os.Getenv("PATH"); //获取系统环境变量 fmt.Printf("%s\n",goos); fmt.Printf("%f\n%d\n",pi,pp); fmt.Printf("%d\n",a); }
六Time Savers : Short Declarations andDistributing
之前定义一个变量方法:
var a int = 10;
为了节约时间,我们可以定义如下:
a := 10;
系统会根据数值的类型定义a这个变量
七基本数据类型
number :
int ,uint ,float ,uintptr
The uintptr data type is a special data typethat is an
unsigned integer that will be large enoughto store a pointer
value.
int ,uint ,float 的长度是根据平台而定的,如果是64bit平台,int和uint长度就是64bit,如果是32 bit平台,它的长度就是32bit;
The architecture-independent numeric typesare:
uint8
uintl6
uint32
uint64
int8
intl6
int32
int64
float32
float64
byte(Thebyte is a familiar alias for uint8.)
NOTE:
{
var a int8 = 10;
var b int16 = 100;
b = a ;
}
This program will generate a compiler error:
cannot use rny16bitint (type int16) as
type int32 in assignment
string :
var str string ;//定义
str = “abcdeft”;
fmt.Printf(str);//输出
str[0] 是‘a’,index从0开始
八 使用strings包
strings.HasPrefix(strstring, prefix string) bool;//判断是否有前缀prefix strings.HasSuffix(strstring, suffix string) bool;//判断是否有后缀suffix strings.Count(strstring,sub string ) int ;//计算有多少个子串sub strings.Index(str string, sub string) int; strings.Lastindex(str string, sub string)int; 如果找不到,return-1 strings.Repeat(str string, count int)string;//复制str几次,返回复制后的字符串 例如:str = “abc”; str1 = strings.Repeat(str,3); //这时str1的值为abcabcabc strings.ToUpper(str string) string; strings.ToLower(str string) string;
九strconvpackage使用
package main import ( "fmt" "strconv" ) func main() { var ii int = 1100; var str2 string; var str1 = "1235"; var err error; ii,err = strconv.Atoi(str1); if err == nil { fmt.Printf("%d\n",ii); } str2 = strconv.Itoa(ii); fmt.Printf(str2); }
十条件语句
//********************************** if condition { } else { } //******************************** if initialization_statement; condition { } else { } //*****switch********************************************** package main import ( "fmt" ) func main() { var a int = 100; switch a { case 100: fmt.Printf("a<100\n"); case 111 : fmt.Printf("a > 111\n"); default: } } //------------------------------------------------------------------------------------------- package main import ( "fmt" ) func main() { var a int = 100; switch { case a< 100: fmt.Printf("a<100\n"); case a > 111 : fmt.Printf("a > 111\n"); default: fmt.Printf("else\n"); } } //***************************************************
十一 循环迭代
/****************************************************************** package main import ( "fmt" ) func main() { for i:= 0 ; i < 100 ; i++ { fmt.Printf("%d ",i); } } //----------------------------------------------------------------------------------------------------------
十二 自定义函数
1. func fun(a int) int {
}
2. func fun(a ,b int ) (int , int ) {
return a,b;
}
在go语言里,是允许多返回值的
十三 使用defer(延缓)
package main import ( "fmt" ) func fun1(){ fmt.Printf("abc\n"); defer fun2(); // 等到fun1 执行结束时,再自动执行fun2(); fmt.Printf("eft\n"); } func fun2() { fmt.Printf("fun2\n"); } func main() { fun1(); }
十四使用blank identifier “_”
package main import ( "fmt" ) func fun1()(int, int ){ return 1,2; } func main() { var a int; a,_ = fun1(); // 用 _ 来记录不要记录的返回值 fmt.Printf("%d\n",a); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。