Go練習(六)

8258 ワード

以下は私がGoを学ぶ时に练习した小さいプログラムの段を记录して、上编に続いて、すでにコンパイルして合格して、ここで记录して、この知识をめくって复习するためにこの知识は前のより少し深くて、もっと重视しなければなりません
//GO Map         map
/*
Map            。  key        ,key     ,      。
  Map               。  Map    ,          ,    map   hash     。
 
   map
         make     map      Map
 --    ,  map nil
 var map_variable map[key_data_type]value_data_type 
--  make  
map_variable :=make(map[key_data_type]value_data_type)

      map,        nil map.nil map         
*/

package main
import "fmt"
func main(){
	var countryCapitalMap map[string]string     //    
	countryCapitalMap =make(map[string]string)  //   

/*map  key-value */
countryCapitalMap["France"]="paris"
countryCapitalMap["Italy"]="  "
countryCapitalMap["Japan"]="  "
countryCapitalMap["India"]="   "

/*        */
for country :=range countryCapitalMap{
	fmt.Println(country,"   ",countryCapitalMap[country])
}

/*            */
capital,ok :=countryCapitalMap["  "]
fmt.Println(capital)
fmt.Println(ok)   //true  false
if(ok){
	fmt.Println("      ",capital)
}else{
	fmt.Println("        ")
	}
}

/*    :
France     paris
Italy       
Japan       
India        

false
        
*/
//delete  
/*         ,   map      key*/
package main
import "fmt"
func main(){
	//  map
	countryCapitalMap :=map[string]string{"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New delhi"}
	fmt.Println("    ")
	//    
	for country :=range countryCapitalMap{
		fmt.Println(country,"   ",countryCapitalMap[country])
	}
	//    
	delete(countryCapitalMap,"France")
	fmt.Println("       ")
	fmt.Println("        ")
	for country:=range countryCapitalMap{
		fmt.Println(country,"   ",countryCapitalMap[country])
	}
}
/*
    :
    
Japan     Tokyo
India     New delhi
France     Paris
Italy     Rome
       
        
India     New delhi
Italy     Rome
Japan     Tokyo
*/
//Go     
//  ,                       
//  :                 

/*
	func recursion(){
		rescursion()        
	}
	func main(){
		recursion()
	}
*/

//  
package main
import "fmt"
func Factorial(n uint64)(result uint64){
	if(n>0){
		result=n*Factorial(n-1)
		return result
	}
	return 1
}
func main(){
	var i int=15
	fmt.Printf("%d    %d
",i,Factorial(uint64(i))) } /* 15 1307674368000 */
//      
//  Go                
package main
import "fmt"
func fibonacci(n int)int{
	if n<2{
		return n	
	}
		return fibonacci(n-1)+fibonacci(n-2)
}
func main(){
	var i int
	for i=0;i<10;i++{
		fmt.Printf("%d\t",fibonacci(i))	
	}
}
/*
0       1       1       2       3       5       8       13      21      34
*/
//Go     
/*
                            。
type_name(expression)
*/

package main
import "fmt"
func main(){
	var sum int=17
	var count int=5
	var mean float32

	mean=float32(sum)/float32(count)
	fmt.Printf("mean   :%f
",mean) } //mean :3.400000
//Go    
//                ,                        

/*
--    
type interface_name interface{
	method_name1[return_type]
	method_name2[return_type]
	...
	method_namen[return_type]
}
--     
tyoe struct_name struct{
	variables
}
--      
func (struct_name_variable struct_name) method_name1()[return_type]{
	    
}
...
func (struct_name_variable struct_name) method_namen()[return_type]{
	    
}
*/

package main
import "fmt"
type Phone interface{
	call()
}
type Huawei struct{

}
func (huawei Huawei) call(){
	fmt.Println("I am Huawei,T can call you")
}
type IPhone struct{

}
func (iphone IPhone) call(){
	fmt.Println("I am IPhone,T can call you")
}
func main(){
	var phone Phone
	phone=new(Huawei)
	phone.call()

	phone=new(IPhone)
	phone.call()
}

/*
I am Huawei,T can call you
I am IPhone,T can call you
*/

package main
import "fmt"
type Man interface{
	name() string
	age() int
}
type Woman struct{

}
func (woman Woman) name() string{
	return "Jim Yawei"
}
func (woman Woman) age() int{
	return 23
}
type Men struct{

}
func (men Men) name() string{
	return "liwen"
}
func (men Men) age() int{
	return 27
}
func main(){
	var man Man
	man=new (Men)
	fmt.Println(man.name())
	fmt.Println(man.age())

	man=new (Woman)
	fmt.Println(man.name())
	fmt.Println(man.age())
}
		/*
			liwen
			27
			Jim Yawei
			23
		*/
//Go     
//       
/*
type err interface{
	Error() string  //       
}
  error           
                    errors.New("")

func Sqrt(f float64)(float64,error){
	if f<0{
		return 0,errors.New("math:square root of negative number")
	}
	  
}

--  
result,error:=Sqrt(-1)
if error!=nil{   -- nil  
	fmt.Println(error)
}
*/

package main
import "fmt"
type DivideError struct{
	dividee int
	divider int
}
//  error  
func (de *DivideError) Error() string{
	strFormat := `
    Cannot proceed, the divider is zero.
    dividee: %d
    divider: 0
`	
return fmt.Sprintf(strFormat,de.dividee)   //  dividee  
}
//  int         
func Divide(varDividee int,varDivider int)(result int,errorMsg string){
	if varDivider==0{
		dData:=DivideError{
			dividee: varDividee,
			divider: varDivider,
		}
		errorMsg=dData.Error()
		return
		}else{
			return varDividee/varDivider,""
		}
}
func main(){
	//    
	if result,errorMsg:=Divide(100,10)
	errorMsg==""{
		fmt.Println("100/10= ",result)
	}
	//                
	if _,errorMsg :=Divide(100,0)
	errorMsg!=""{
		fmt.Println("errorMsg is: ",errorMsg)
	}
}

		/*
		    :
		100/10=  10
		errorMsg is:
		    Cannot proceed, the divider is zero.
		    dividee: 100
		    divider: 0

		*/
//panic  recover
//  :          ,      panic     
/*
      :    Go      ,Panic        ,recover    panic     
1   panic     :        ,            ,         

2   panic ,      panic        panic       ,         defer  ,            ,   recover            

3 panic              , defer           panic   panic.defer   panic        defer  

4 recover    panic,  panic      。recover() defer    ,  defer                   panic     ,    nil,        。

    

            :

    1.              ,    ,      。
    
    2.      ,   panic      ,      。

*/

//      
defer recover()
defer fmt.Println(recover)
defer func(){
	func(){
		recover()  //  ,    
	}()
}()

//      
defer func(){
	recover()
}()
func except(){
	recover()
}
func test(){
	defer except()
	panic("runtime error")
}

//  panic        
package main
import "fmt"
func main(){
	defer func(){
		if err:=recover()
		err!=nil{
			fmt.Println(err)
		}
	}()
	defer func(){
		panic("three")
	}()
	defer func(){
		panic("two")
	}()
	panic("one")
}
//Go    
//Go      ,  go      goroutine  
/*
goroutine      ,goroutine     Golang       
    
---go    (    )

    go             , goroutine,          goroutine       。
         goroutine        
*/

package main
import "fmt"
import "time"
func say(s string){
	for i := 0; i < 6; i++ {
		time.Sleep(100*time.Millisecond)
		fmt.Println(s)	
	}
}
func main(){
	go say("world")   //         
	say("hello")
}
/*
      ,
       hello   world          。
        goroutine    
*/

	/*
	world
	hello
	world
	hello
	world
	hello
	world
	hello
	world
	hello
	*/
//    Channel
/*
   
             
       goroutine                       
   
//Go      
/*
         ,  make              
ch :=make(chan int,100)

                                ,                  ,            ,                 
              ,             。       ,              。
  :
        ,                    
     ,                     ;       ,                     。                 。
*/

package main
import "fmt"
func main(){
//                      
ch :=make(chan int,2)
//  ch       ,            ,              
ch
//GO            
/*
Go  range              ,        
v,ok :=