Go language learning Day05

   Separated for a long time to learn to start writing this blog, I feel some shame, really middle of the Go programming learning time can not be broken, a broken it is easy to disconnect, then briefly explain what things learned today.

1.Go language representation method, since the representation of the language itself is not similar Go Class this class, so its methods, mainly in the form of a structure, to achieve.

The method represented by general format: func (structure) method name () {return value

Concrete implementation

}

Sample code:

//go语言方法
package main

import (
	"fmt"
	"math"
)

type Vertex struct {
	X,Y float64
}

//类似于结构体的方法
func (v Vertex) Abs() float64{
	return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main(){
	v:=Vertex{3,4} //定义一个结构体
	fmt.Println(v.Abs()) //调用结构体的Abs方法
}


Another representation method is that the front structure type, can be replaced by any type, so that facilitate scalability in Go, I have to say that the design is very clever.

Sample code:

//type可以自定义数据类型
//语法 type typename datatype
package main

import (
	"fmt"
	"math"
)

type MyFloat float64

func (f MyFloat) Abs() float64{
	if f < 0{
		return float64(-f)
	}
	return float64(f)
}

func main(){
	f:= MyFloat(-math.Sqrt2)
	fmt.Println(f.Abs())
}

 

2. The method structure as application parameters:

This is relatively simple, the sample code can be understood

Sample code:

//方法其实就是一个带有参数的函数
package main

import (
	"fmt"
	"math"
)

type Vertex struct {
	X,Y float64
}

func Abs(v Vertex) float64{
	return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main(){
	v:=Vertex{3,4}
	fmt.Println(Abs(v))
}

3. The structure parameters do use the pointer, and the wording of this method of body structure is exactly the same, except that the meaning is not the same.

Sample code:

//指针的话,也有类似的用法 只是意义不一样,表示形式都是一样的
package main

import (
	"fmt"
	"math"
)

type Vertex struct {
	X,Y float64
}

func (v Vertex) Abs()float64{
	return math.Sqrt(v.X*v.X +v.Y*v.Y)
}

func (v *Vertex) Scale(f float64){
	v.X = v.X * f
	v.Y = v.Y * f
}

func main(){
	v:=Vertex{3,4}
	v.Scale(10)
	fmt.Println(v.Abs())
}

Selected value or a pointer as a receiver process

//选择值或指针作为接收者
package main

import (
	"fmt"
	"math"
)

type Vertex struct {
	X,Y float64
}

func (v *Vertex) Scale (f float64){
	v.X = v.X * f
	v.Y = v.Y * f
}

func (v *Vertex)Abs()float64  {
	return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main()  {
	v := &Vertex{3,4}
	fmt.Printf("Before scaling: %+v,Abs: %v\n",v,v.Abs())
	v.Scale(5)
	fmt.Printf("After scaling: %+v,Abs:%v\n",v,v.Abs())
}

4. pointer redirection: This may be more difficult to understand the concept, but it plainly, is to take the address of the essence, because every change no matter what the address is the same, but the value will point to the memory address may change, understanding after this concept, the subsequent code is relatively easy

Sample code:

//指针重定向
package main

import "fmt"

type Vertex struct {
	X,Y float64
}

func (v *Vertex) Scale(f float64){
	v.X = v.X*f
	v.Y = v.Y*f
}

func ScaleFunc(v *Vertex,f float64){
	v.X = v.X*f
	v.Y = v.Y*f
}

func main(){
	v:=Vertex{4,3}
	v.Scale(3)
	ScaleFunc(&v,10)
	p:= &Vertex{4,3}
	p.Scale(3)
	ScaleFunc(p,8)

	fmt.Println(v,p)
}

package main

import (
	"fmt"
	"math"
)

type Vertex struct{
	X,Y float64
}

func (v Vertex) Abs()float64{
	return math.Sqrt(v.X * v.X + v.X * v.Y)
}

func AbsFunc(v Vertex) float64{
	return math.Sqrt(v.X * v.X + v.Y * v.Y)
}

func main(){
	v:=Vertex{3,4}
	fmt.Println(v.Abs())
	fmt.Println(AbsFunc(v))

	//取地址也是用. 切记 内部原因待查
	p:= &Vertex{4,3}
	fmt.Println(p.Abs())
	fmt.Println(AbsFunc(*p))
}

These being the first to learn, continue to follow-up summary, certain things can not be delayed, otherwise it will only prolong, this is my little experience! It is important to overcome procrastination

Guess you like

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