当前位置:网站首页>Use of go function
Use of go function
2022-04-21 10:42:00 【genispan】
be familiar with Go The syntax structure of a function
because GO There is no concept of a class , So use structure to simulate . The function in the code calculates the square root of two integers and takes the absolute value of the value .
package main
import (
"fmt"
"math"
)
type MyFloat float64
type Vertex struct {
X, Y float64
}
//Vertex The pointer corresponds to Abs Method
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
//MyFloat Structure corresponding to Abs Method
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
func main() {
v := &Vertex{6, 4} // obtain Vertex Pointer to structure
fmt.Println(v.Abs())
f := MyFloat(-math.Sqrt2) // take 2 The square root of
fmt.Println("float Abs=", f.Abs())
}
The meaning of defining the corresponding method here is that it operates on the original object . If it is defined as the method corresponding to the structure type , It means that the passed parameter is a copy of the structure , Not the original object . We can add Add Method , As shown below :
package main
import (
"fmt"
"math"
)
type MyFloat float64
type Vertex struct {
X, Y float64
}
//Vertex The pointer corresponds to Abs Method
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
//Vertex The pointer corresponds to Add Method
func (v *Vertex) Add(f float64) {
v.X += f
v.Y += f
}
//MyFloat Structure corresponding to Abs Method
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
func main() {
v := &Vertex{6, 4} // obtain Vertex Pointer to structure
fmt.Println(v.Abs())
v.Add(3.14)
fmt.Println(v)
f := MyFloat(-math.Sqrt2) // take 2 The square root of
fmt.Println("float Abs=", f.Abs())
}
After running , object v Inside X and Y The value of .
版权声明
本文为[genispan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211042160403.html
边栏推荐
- zsh: segmentation fault 解决方法
- 京东物流、日日顺供应链、顺丰们能否在“疫”下找到物流的最优解?
- pip install的时候出现问题,无法连接...(pip配置镜像源)
- 現代精算風險理論07:風險度量
- TypeError: The view function did not return a valid response. The function either returned None 的解决
- “空气洗”再迎迭代,模仿者又有了新目标
- Recursive function C language question type
- Microsoft updates the verifier application for Android / IOS to support the generation of more secure strong passwords
- Copyright loopholes in NFT: product design needs to consider the legal level
- Go环境的搭建
猜你喜欢

AcWing 1737. 传送(分类讨论)

MKL and vs2019 configuration method

Pytorch梯度检查 torch.autograd.gradcheck

"Air washing" meets the iteration again, and the imitator has a new goal

Leetcode 110 Balanced binary tree (2022.04.20)

AOT和单文件发布对程序性能的影响

24 pictures to conquer border image

Convbert: improving Bert with span based dynamic revolution

24张图攻克border-image

MySQL深入学习(三十):数据库的设计规范
随机推荐
浏览器插件(BD新标签页)壁纸鉴赏
TypeError: The view function did not return a valid response. The function either returned none
Android学习 ① Android连接不上Mysql数据库的多种原因以及解决方式
Convbert: improving Bert with span based dynamic revolution
Project scheme of shopping guide system based on Runhe Dayu development board
How to write product requirements document (PRD) with the idea of five elements of user experience
本地IP地址使用域名访问
MKL and vs2019 configuration method
你不知道的 parseInt?
小程序生命周期
After reading this article, I'll teach you to play with the penetration test target vulnhub - dc9
二分查找符合要求的值及局部最小值
DFS of vigorously flying brick (creation of tree)
A collection of econometric models and empirical Stata codes, with top publication examples
What happens when you run the NPM install command?
Filebeat收集日志数据传输到Redis,通过Logstash来根据日志字段创建不同的ES索引
【pytorch】实现1对多的IOU计算技巧
postman设置环境变量,简单又实用
shell脚本修改文件名和权限
润和大禹学习方案