Go language learning Day03

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Giser_D/article/details/90709641

Today is the third day of learning go, get up at 6 am, sleep a little late, I do not hope, and then isolating the phone, it will find that life is really good.

Today, we are learning for, if, switch, defer such phrases

A, for circulation;

for circulating form is for variable declaration; loop termination condition; {

     Implementation

}

//学习for语句
//for语句通用格式跟C++很像,主要是不加括号和变量声明
//但是需要大括号
package main

import "fmt"

func main(){
	sum:= 0
	for i:=0;i<10;i++{
		sum+=i
	}
	fmt.Println(sum)
}

Cycling conditions may be omitted, but the first need to add semi-colon 

 

package main

import "fmt"

func main(){
	sum:= 1
	for ;sum < 10;sum++{
		sum+=sum
	}
	fmt.Println(sum)
}

 

//for循环,无条件循环
package main

func main()  {
	for{
		}
}

 Two, if statements

if statement written in other languages ​​and writing are basically the same.

Then focus on the front if do not need parentheses

 

//if语句写法:
package main

import (
	"fmt"
	"math"
)
//fmt.Sprint:输出字符串

func sqrt(x float64) string {
	if x < 0{
		return sqrt(-x) + "i"
	}
	return fmt.Sprint(math.Sqrt(x))
}

func main(){
	fmt.Println(sqrt(36))
}

Variables declared inside an if statement as follows: 

 

package main

import (
	"fmt"
	"math"
)

func pows(x,n,lim float64)float64{
	if v:=math.Pow(x,n);v<lim{
		return v
	}else{
		fmt.Printf("%g >= %g\n",v,lim)
	}
	//这里开始不能使用v v只能在if和else 判断语句中使用
	return lim
}

func main(){
	fmt.Println("当前结果为",pows(2,3,4))
}

 

package main

import (
	"fmt"
)

func Sqrt(x float64)float64{
	//实现平方根函数
	var z float64=1 //声明变量
	for i:=0;i<10;i++{
		z = z - (z*z-x)/(2*z)
		fmt.Println(z,"\n")
	}
	return 0
}

func main(){
	Sqrt(2)
}

Three, Switch statement:

The switch statement can be omitted objects switch, and this is very Niubi, go feeling is the feeling you could save as much as possible the language of the province. 

 

package main

import (
	"fmt"
	"time"
)

//switch 可以省略条件语句,更加的go写法
func main(){
	t:=time.Now()
	switch  {
	case t.Hour() <12:
		fmt.Println("Hwllo")
	case t.Hour() < 17:
		fmt.Println("ddd")
	default:
		fmt.Println("OK")
	}
}

 

package main

import (
	"fmt"
	"math"
)

//if语句这边可以先进行赋值操作,然后再进行if比较(特性)
func pow(x,n,lim float64) float64 {
	if v:=math.Pow(x,n);v<lim{
		return v
	}
	return lim
}

func main(){
	fmt.Println(
		pow(3,2,10),
		pow(1,4,19),
		)
}
//学习switch语句
package main

import (
	"fmt"
	"runtime"
)

//runtime.GOOS 表示获取当前运行环境的操作系统
//switch语法比较像if语句,均可以在开头申明变量,用;隔开
func main(){
	fmt.Println("Go runs on ")
	switch  os := runtime.GOOS;os{
	case "darwin":
		fmt.Println("OS X.")
	case "linux":
		fmt.Println("Linux")
	default:
		fmt.Printf("%s \n",os)
	}
}

package main

import (
	"fmt"
	"time"
)

func main(){
	fmt.Println("When's Saturday")
	today:= time.Now().Weekday()
	switch time.Saturday {
	case today+0:
		fmt.Println("Today.")
	case today+1:
		fmt.Println("Tomorrow")
	case today+2:
		fmt.Println("In two days")
	default:
		fmt.Println("Too far away")

	}
}

Five, While statement: This is also a feature of the Go language, While the statement is actually written statement for the removal of two semicolons, with judgment criteria: the feeling this is one of the soul go, after all, not concurrent study. 

 

//不加分号的for语句指的是while语句
package main

import "fmt"

func main()  {
	sum:=1
	for sum<23{
		sum+=3
	}
	fmt.Println(sum)
}

Six, Defer statement:

defer execution method 

//defer语句用法,就是defer后面跟的语句,会在外层函数执行完后执行
package main

import "fmt"

func main(){
	defer fmt.Println("world")
	fmt.Println("hello")
}

 When more than defer statements, follow the principle of LIFO!

package main

//多个defer遵循后进先出原则
import "fmt"

func main(){
	fmt.Println("counting")
	for i:=0;i<10;i++{
		defer fmt.Println(i)
	}
	fmt.Println("done!")
}

 

Have a chance to learn, mainly I am more stupid

Guess you like

Origin blog.csdn.net/Giser_D/article/details/90709641