go语言学习 四

  1 package main                                                                     

  2                                                                                  

  3 import "fmt"                                                                     

  4                                                                                  

  5 type rest struct{                                                                

  6     width,height int                                                             

  7 }                                                                                

  8                                                                                  

  9 func (r *rest) area() int {                                                      

 10     return r.width*r.height                                                      

 11 }                                                                                

 12                                                                                  

 13 func (r *rest) peri() int {                                                      

 14     return 2*(r.width+r.height)                                                  

 15 }                                                                                

 16                                                                                  

 17 func main(){                                                                     

 18     r:=rest{}                                                                    

 19     fmt.Scanf("%d",&r.width)                                                     

 20     fmt.Scanf("%d",&r.height)                                                    

 21                                                                                  

 22     fmt.Println(r.area())                                                        

 23     fmt.Println(r.peri())                                                        

 24 }

~                        

之前介绍过一次go语言里边结构体的应用,这次用结构体实现了结构体对应的函数 通过指针来传参,在调用的时候和c里边类似直接结构体实例名称.函数就可以了。之后我们要讲的是go语言里边接口的概念,接口就类似于我们c++里边的多态 看程序

 1 package main                                                                                                                                                                                                                                                               

  2                                                                                  

  3 import "fmt"                                                                     

  4 import "math"                                                                   

  5                                                                                  

  6 type shape interface {                                                           

  7     area() float64                                                              

  8     peri() float64                                                               

  9 }                                                                                

 10                                                                                  

 11 //长方形                                                                         

 12                                                                                  

 13 type rect struct {                                                               

 14     width,height float64                                                         

 15 }                                                                                

 16 func (r *rect) area() float64{                                                   

 17     return r.width*r.height                                                      

 18 }                                                                                

 19                                                                                  

 20 func (r *rect) peri() float64{                                                   

 21     return 2*(r.width+r.height)                                                  

 22 }                                                                                

 23                                                                                  

 24 type circle struct{                                                              

 25     radius float64                                                               

 26 }                                                                                

 27                                                                                  

 28 func (c *circle) area() float64 {                                                

 29     return math.Pi*c.radius*c.radius                                             

 30 }                                                                                

 31                                                                                  

 32 func (c *circle) peri() float64{                                                 

 33     return 2*math.Pi*c.radius                                                    

 34 }                                                                                

 35                                                                                  

 36 func main(){                                                                     

 37     r:=rect {width:2.9,height:4.8}                                               

 38     c:=circle{radius:4.3}                                                        

 39                                                                                  

 40     s:=[]shape{&r,&c}                                                            

 41                                                                                  

 42     for _,sh:=range s{                                                           

 43         fmt.Println(sh)                                                          

 44         fmt.Println(sh.area())                                                   

 45         fmt.Println(sh.peri())                                                   

 46     }                                                                            

 47 }

这里我们定义了两种结构体一种是圆形 一种是正方形通过定义了interface接口来实现多态不同的结构体虽然调用的函数名称是一样的,但是他们都调用了属于自己的那一套接口。之后在主函数里通过数组来访问存储各个结构体的指针

 

可以看到不同的结构体调用的不同的解决方法

 

错误处理,昨天我们讲了在go语言中可以用多个返回值来判断是否成功 在c中我们常常遇到的问题就是内存泄漏的问题,有时候内存泄漏是我们自己忘记释放,但是有时候并不是我们不小心内存泄漏的,比如c++里边我们引入了异常的概念,当我们在执行程序的时候很有可能我们申请了内存之后还没来得及释放就跳转到异常内部执行了,所以c++中有了RAII智能指针来解决这个问题。go语言则引入了defer来确保那些被打开的文件等东西被关闭,

 

func CopyFile(dstName, srcName string) (written int64, err error) {

    src, err := os.Open(srcName)

    if err != nil {

        return

    }

    defer src.Close()

    dst, err := os.Create(dstName)

    if err != nil {

        return

    }

    defer dst.Close()

    return io.Copy(dst, src)

}

defer在go语言里边是延时函数,当在copyfile函数里边调用defer的时候,defer后边的函数 例如src.close这些函数会在copyfile函数执行完成之后在再执行。这样就能保证开的文件正常的关闭掉并且延时函数是后进先出的顺序执行的。比如上边我们注册了两个defer函数 一个是src的close一个是dst的close因为src的注册在前,所以执行的时候先执行dst的close

在go语言中有一个panic的概念,panic的英文原意为恐慌 惊慌 panic在go语言里边是用来表示非常严重的不可恢复的错误,在go语言里边这事一个内置函数,接受一个任何类型的值作为参数,panic作用就像我们平常接触的异常,go里边没有tyr catch 所以panic一般来说都会导致程序挂掉,这里关键的一点是,即使函数执行的时候panic了函数不往下走了,并不是立刻向上传递panic二十到defer那,等defer的函数跑完,这时候再去传递Panic

但是 go语言中也有recover 类似于try catch,上边说了 panic的函数不会立刻返回,而是先defer 再返回这时候 如果有办法捕获panic 并且阻止异常的传递 recover就是这个,一旦panic逻辑就会走到defer那,我们就在那等着调用recover函数来捕获当前的panic

 

已标记关键词 清除标记
相关推荐
<p style="box-sizing: border-box; margin: 0px; padding: 0px; font-family: 'PingFang SC', 'Hiragino Sans GB', Arial, 'Microsoft YaHei', Verdana, Roboto, Noto, 'Helvetica Neue', sans-serif; color: #222226; font-size: 14px; font-variant-ligatures: common-ligatures; background-color: #ffffff;">Go富有表现力,简洁,整洁且高效。它的并发机制使编写程序可以轻松地从多核和联网机器中获得最大收益,而其新颖的类型系统则可以实现灵活的模块化程序构造。Go可以快速编译为机器代码,但具有垃圾回收的便利性和运行时反射的功能。它是一种快速的,静态类型的编译语言,感觉就像是一种动态类型的解释语言。</p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; font-family: 'PingFang SC', 'Hiragino Sans GB', Arial, 'Microsoft YaHei', Verdana, Roboto, Noto, 'Helvetica Neue', sans-serif; color: #222226; font-size: 14px; font-variant-ligatures: common-ligatures; background-color: #ffffff;">Go语言官方文档学笔记是基于官方文档及个人学Go的笔记,整理完成的Go语言快速入门课程。第四季内容包括以下十个部分:</p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">1-Forever</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">2-If</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">3-If with a short statement</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">4-If and else</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">5-Switch</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">6-Switch evaluation order</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">7-Switch with no condition</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">8-Defer</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">9-Stacking defers</span></span></p> <p style="box-sizing: border-box; margin: 0px; padding: 0px; background-color: #ffffff;"><span style="color: #222226; font-family: PingFang SC, Hiragino Sans GB, Arial, Microsoft YaHei, Verdana, Roboto, Noto, Helvetica Neue, sans-serif;"><span style="font-size: 14px;">10-Pointers</span></span></p>
<p style="text-align:center;"> <strong><span style="font-size:32px;"><br /> </span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;color:#337FE5;">程序员转行首选Go </span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;color:#337FE5;">入职大厂快人一步</span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><span style="color:#337FE5;"><br /> </span></span></strong> </p> <p style="text-align:left;"> <span style="font-size:16px;">Go被称为“21世纪的C语言”,因其简洁的语法,以及自带的内存回收机制,使其可以拥有高速运</span><span style="font-size:16px;">行效率的同时,还能兼顾开发效率,所以越来越多大厂开始转投Go语言!</span> </p> <p style="text-align:center;"> <br /> </p> <p style="text-align:center;"> <br /> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;color:#337FE5;"><strong><span style="font-size:18px;color:#337FE5;">转行学误区多 </span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;color:#337FE5;"><strong><span style="font-size:18px;color:#337FE5;">学效率Down到底</span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;color:#337FE5;"><br /> </span></strong> </p> <p style="text-align:left;"> <span style="font-size:16px;">在实际的开发工作中,有Java、PHP、Python、C/C++等编程语言基础的程序员,由于固有编程</span><span style="font-size:16px;">惯的限制,很容易陷入学误区,学效率大大降低!</span> </p> <p style="text-align:left;"> <span style="font-size:16px;"><br /> </span> </p> <p style="text-align:center;"> <br /> </p> <p style="text-align:left;"> <span style="font-size:18px;"><img src="https://img-bss.csdnimg.cn/202011170520559519.png" alt="" /><br /> </span> </p> <p style="text-align:left;"> <span style="font-size:18px;"><br /> </span> </p> <p style="text-align:left;"> <span style="font-size:18px;"><br /> </span> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;color:#337FE5;">站内首门专为转行打造 </span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;color:#337FE5;">程序员学Go不踩坑</span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><span style="color:#337FE5;"><br /> </span></span></strong> </p> <p style="text-align:left;"> <span style="font-size:16px;">《快速入门Go语言》,是站内首门专为程序员转行Go开发打造的必修课,课程详细讲解Go语言</span><span style="font-size:16px;">中必备的核心语法,以及Go中最重要的并发编程、切片及数组的使用等内容!</span> </p> <p style="text-align:left;"> <span style="font-size:16px;"><br /> </span> </p> <p style="text-align:center;"> <br /> </p> <p style="text-align:left;"> <span style="font-size:18px;"><img src="https://img-bss.csdnimg.cn/202011170522289181.png" alt="" /><br /> </span> </p> <p style="text-align:left;"> <span style="font-size:18px;"><br /> </span> </p> <p style="text-align:left;"> <span style="font-size:18px;"><br /> </span> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;color:#337FE5;">课程逐级进阶 </span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;color:#337FE5;">快速提升Go语言编程思维</span></strong></span></strong> </p> <p style="text-align:left;"> <strong><span style="font-size:18px;"><span style="color:#337FE5;"><br /> </span></span></strong> </p> <p style="text-align:left;"> <span style="font-size:16px;">课程按知识点内容划分为先导篇、语法篇并发篇、网络篇、专项进阶篇、数据库编程篇,不只将</span><span style="font-size:16px;">知识点讲解透彻,更讲透为什么用、如何用,提升程序员Go语言的编程思维!</span> </p> <p style="text-align:left;"> <span style="font-size:16px;"><br /> </span> </p> <p style="text-align:center;"> <br /> </p> <p style="text-align:left;"> <span style="font-size:18px;"><img src="https://img-bss.csdnimg.cn/202011170523456090.png" alt="" /><br /> </span> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页