go学习笔记

安装

brew install go

国际惯例hello,world.

创建文件hello.go
go文件的main方法为函数的主入口,必须有这个方法。
hello.go

package main 
import "fmt"
func  main() {
	fmt.Println("hello,go")
}

编译并运行文件

首先编译

go build hello.go

然后运行

go run hello.go

编译一次即可。

输入一个数字并运算

package main 
import (
"fmt"
"math"
)

func main() {
	fmt.Println("Circle Area calculation")
	fmt.Print("Enter a radius value: ")
	var radius float64
	fmt.Scanf("%f", &radius) #可以接受控制台输入的数字
	area := math.Pi * math.Pow(radius,2)
	fmt.Printf("Circle area with radius %.2f = %.2f \n", radius,area)
}

变量声明和赋值

package  main
import "fmt"
func main() {
	// 声明变量,需要制定类型。
	var str string
	var n,m int
	var mn float32

	// 赋值
	str="Hello World"
	n=10
	m=50
	mn=2.45
	fmt.Println("value of str= ",str)
	fmt.Println("value of n=",n)
	fmt.Println("value of m=",m)
	fmt.Println("value of mn=",mn)

	// 声明变量并赋值
	var city string="London"
	var x int =10
	fmt.Println("value of city=",city)
	fmt.Println("value of x=",x)

	// 声明变量并赋默认值
	contry :="DE" 
	contry="Hello"
	val:=15
	fmt.Println("value of contry",contry)
	fmt.Println("value of val=",val)
	/* 声明多个变量 */
	var (
        name string
        email string
        age int 
		)
	name = "john"
	email = "john@email.com"
	age = 25
	fmt.Println(name)
	fmt.Println(email)
	fmt.Println(age)

}

加减乘除

package main 
import "fmt"
func main() {
	// declare variables
	var a,b int 
	// assign values
	a = 5
	b = 10
	// arithmetic operation
	// addition
	c:=a + b
	fmt.Printf("%d + %d = %d \n",a,b,c)
	// subtraction
	d:=a - b
	fmt.Printf("%d - %d = %d \n",a,b,d)
	// division
	e:=float32(a) / float32(b)
	fmt.Printf("%d / %d = %.2f \n",a,b,e)
	// multiplication
	f:=a * b
	fmt.Printf("%d * %d = %d \n",a,b,f)
}

数学运算

package main 
import (
"fmt"
"math"
)

func main() {
	a := 2.4
	b := 1.6
	c := math.Pow(a,2)
	fmt.Printf("%.2f^%d = %.2f \n",a,2,c)

	c = math.Sin(a)
	fmt.Printf("Sin(%.2f) = %.2f \n",a,c)

	c = math.Cos(b)
	fmt.Printf("Cos(%.2f) = %.2f \n",b,c)

	c = math.Sqrt(a*b)
	fmt.Printf("Sqrt(%.2f*%.2f) = %.2f \n",a,b,c)

}

条件语句if

package main 
import "fmt"
func main() {
	var (
      a=5
      b=8
		)
	/*
if{ #这个花括号必须紧邻if
	do something
}else{ #else前后紧邻}和{
	do something
}
}
	*/
	if a>b || a-b<a {
		fmt.Println("condtional-->a>b || a-b<a")
	}else{

		fmt.Println("...another")
	}
}

自增和自减

package main 
import "fmt"
func main() {
	// declare variables
	var a = 4

	// increment
	fmt.Printf("a = %d \n",a)
	a=a+1
	fmt.Printf("a+1 = %d \n",a)
	a++
	fmt.Printf("a++ = %d \n",a)

	// decrement
	a = a - 1
	fmt.Printf("a - 1 = %d \n",a)
	a--
	fmt.Printf("a-- = %d \n",a)

}

switch..case

/*
switch option{
	case option1:
	// do option1 job
	case option2:
	// do option2 job
}
*/
package main 

import "fmt"

func main() {
 selected := 2
 switch selected{
     case 0:
     	fmt.Println("selected = 0")
     case 1:
         fmt.Println("selected = 1")
     default:
         fmt.Println("other..")    	

 }
	
}

for循环

/*
for initialization;condition;increment/decrement{
	
}
*/
package main 

import "fmt"

func main() {

	  // iteration -for	
	  var i int 
	  for i=0;i<5;i++{
		fmt.Println(i)
	  }

	  for j:=5;j<11;j++{
		fmt.Println(j)
	  }
}

posted @ 2019-01-31 17:58  公众号python学习开发  阅读(247)  评论(0编辑  收藏  举报