当前位置: 首页 > news >正文

用Golang实现图形面积计算

计算正方形面积

package mainimport "fmt"type square struct {side float32
}func (s square) area() float32 {return s.side * s.side
}func main() {s := square{side: 10}fmt.Printf("%T\n", s)fmt.Println(s.area())
}

计算正方形、圆形、矩形面积,写一个通用的函数info和一个通用的interface

package mainimport "fmt"
import "math"type square struct {side float32
}func (s square) area() float32 {return s.side * s.side
}type circle struct {radius float32
}func (c circle) area() float32 {return math.Pi * c.radius * c.radius
}type rectangle struct {width  float32length float32
}func (r rectangle) area() float32 {return r.width * r.length
}type shape interface {area() float32
}func info(sp shape) {fmt.Println("shape area : ", sp.area())
}func main() {s := square{side: 10}c := circle{radius: 10}r := rectangle{width: 10, length: 10}info(s) // 打印 shape area :  100info(c) // 打印 shape area :  314.15927info(r) // 打印 shape area :  100
}

http://www.mrgr.cn/news/3667.html

相关文章:

  • 字节显示转换操作类 - C#小函数类推荐
  • Zotero 常用插件介绍
  • Java-HttpHeaders请求头或响应头
  • xfs Vs ext4?
  • c语言利用三目运算符制作订单判断免运费程序
  • Windows 11 24H2 终于允许多个应用程序同时使用摄像头
  • Mac安装Typora
  • 用户和实体行为分析(UEBA)基础指南及解决方案
  • 2024.8.21
  • 34.给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。要求算法时间复杂度是 O(log n)
  • 51单片机学习
  • 未设置辅助手机的谷歌账号停用,申诉回来后登录需要手机验证,验证两次后成功恢复。
  • 【Axure视频教程】中继器表格——设置文字颜色
  • C++ 设计模式——建造者模式
  • mysql在k8s环境里安装及搭建主从架构
  • 【Java学习】方法的引用
  • js实现点击图片放大效果
  • 开始尝试从0写一个项目--前端(四)
  • JavaEE:http请求 | 同步与异步请求 | 跨域问题 | axios框架 有这一篇就够!
  • 《中国档案》