zoukankan      html  css  js  c++  java
  • 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)
    	  }
    }
    
    
  • 相关阅读:
    React—ref 属性
    【支付宝SDK】沙箱调试,以及遇到的坑
    python 字符串相乘
    微信小程序授权获取手机号 出现-41003错误
    微信小程序 动态添加 view input picker
    PHP 获取当月的第一天和最后一天
    微信小程序 弹窗组件
    微信小程序 图片等比例 适应屏幕尺寸
    微信小程序图片预览
    div背景图占满
  • 原文地址:https://www.cnblogs.com/c-x-a/p/10343066.html
Copyright © 2011-2022 走看看