• 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 
    ", 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 
    ",a,b,c)
    	// subtraction
    	d:=a - b
    	fmt.Printf("%d - %d = %d 
    ",a,b,d)
    	// division
    	e:=float32(a) / float32(b)
    	fmt.Printf("%d / %d = %.2f 
    ",a,b,e)
    	// multiplication
    	f:=a * b
    	fmt.Printf("%d * %d = %d 
    ",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 
    ",a,2,c)
    
    	c = math.Sin(a)
    	fmt.Printf("Sin(%.2f) = %.2f 
    ",a,c)
    
    	c = math.Cos(b)
    	fmt.Printf("Cos(%.2f) = %.2f 
    ",b,c)
    
    	c = math.Sqrt(a*b)
    	fmt.Printf("Sqrt(%.2f*%.2f) = %.2f 
    ",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 
    ",a)
    	a=a+1
    	fmt.Printf("a+1 = %d 
    ",a)
    	a++
    	fmt.Printf("a++ = %d 
    ",a)
    
    	// decrement
    	a = a - 1
    	fmt.Printf("a - 1 = %d 
    ",a)
    	a--
    	fmt.Printf("a-- = %d 
    ",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)
    	  }
    }
    
    
  • 相关阅读:
    git代码冲突
    Centos Git1.7.1升级到Git2.2.1
    linux指定某非root用户执行开机启动项的方法(gogs git)
    kvm增加硬盘挂载
    git分支管理策略
    mac命令行配置网络
    svn稀疏目录--通过设置工作目录的深度(depth)实现目录树的部分签出
    svn update解决冲突
    rocketmq单机搭建
    MongoDB数据库未授权访问漏洞及加固
  • 原文地址:https://www.cnblogs.com/c-x-a/p/10343066.html
Copyright © 2020-2023  润新知