其他分享
首页 > 其他分享> > function-

function-

作者:互联网

function

1 值类型或者指针类型都可以互相调用值接受者或者指针接受者

2 是否改变原来的值 取决于接受者是值类型还是指针类型

// Sample program to show how to declare methods and how the Go
// compiler supports them.
package main

import (
	"fmt"
)

// user defines a user in the program.
type user struct {
	name  string
	email string
}

// notify implements a method with a value receiver.
func (u user) notify() {
	fmt.Printf("Sending User Email To %s<%s>\n",
		u.name,
		u.email)
}

// changeEmail implements a method with a pointer receiver.
func (u *user) changeEmail(email string) {
	u.email = email
}

// main is the entry point for the application.
func main() {
	// Values of type user can be used to call methods
	// declared with a value receiver.
	bill := user{"Bill", "bill@email.com"}
	bill.notify()

	// Pointers of type user can also be used to call methods
	// declared with a value receiver.
	lisa := &user{"Lisa", "lisa@email.com"}
	lisa.notify()

	// Values of type user can be used to call methods
	// declared with a pointer receiver.
	bill.changeEmail("bill@gmail.com")
	bill.notify()

	// Pointers of type user can be used to call methods
	// declared with a pointer receiver.
	lisa.changeEmail("lisa@gmail.com")
	lisa.notify()
}
// Sample program to show how to declare methods against
// a named type.
package main

import (
	"fmt"
)

// duration is a named type that represents a duration
// of time in Nanosecond.
type duration int64

const (
	nanosecond  duration = 1
	microsecond          = 1000 * nanosecond
	millisecond          = 1000 * microsecond
	second               = 1000 * millisecond
	minute               = 60 * second
	hour                 = 60 * minute
)

// setHours sets the specified number of hours.
func (d *duration) setHours(h float64) {
	*d = duration(h) * hour
}

// hours returns the duration as a floating point number of hours.
func (d duration) hours() float64 {
	hour := d / hour
	nsec := d % hour
	return float64(hour) + float64(nsec)*(1e-9/60/60)
}

// main is the entry point for the application.
func main() {
	// Declare a variable of type duration set to
	// its zero value.
	var dur duration

	// Change the value of dur to equal
	// five seconds.
	dur.setHours(5)

	// Display the new value of dur.
	fmt.Println("Hours:", dur.hours())
}
// Sample program to show how to declare function variables.
package main

import "fmt"

// data is a struct to bind methods to.
type data struct {
	name string
	age  int
}

// displayName provides a pretty print view of the name.
func (d data) displayName() {
	fmt.Println("My Name Is: ", d.name)
}

// setAge sets the age and displays the value.
func (d *data) setAge(age int) {
	d.age = age
	fmt.Println("Set Age: ", d.age)
}

// main is the entry point for the application.
func main() {
	// Declare a variable of type data.
	d := data{
		name: "Bill",
	}

	// Declare a function variable for the method
	// bound to the d variable.
	f1 := d.displayName

	// Call the method via the variable.
	f1()

	// Declare a function variable for the function
	// bound to the package.
	f2 := data.displayName

	// Call the function passing the receiver.
	f2(d)

	// Declare a function variable for the method
	// bound to the d variable.
	f3 := d.setAge

	// Call the method via the variable passing the parameter.
	f3(45)

	// Declare a function variable for the function
	// bound to the package. The receiver is a pointer.
	f4 := (*data).setAge

	// Call the function passing the receiver and the parameter.
	f4(&d, 55)
}

函数类型(function types)

是一种很特殊的类型,它表示着所有拥有同样的入参类型和返回值类型的函数集合。

如下这一行代码,定义了一个名叫 Greeting 的函数类型

type Greeting func(name string) string

这种类型有两个特征:

  1. 只接收一个参数 ,并且该参数的类型为 string

  2. 返回值也只有一个参数,其类型为 string

一个函数只要满足这些特征,那么它就可以通过如下方式将该函数转换成 Greeting 类型的函数对象(也即 greet)

func english(name string) string {
    return "Hello, " + name
}

// 转换成 Greeting 类型的函数对象
greet := Greeting(english)
// 或者
var greet Greeting = english

greet 做为 Greeting 类型的对象,也拥有 Greeting 类型的所有方法,比如下面的 say 方法

func (g Greeting) say(n string) {
    fmt.Println(g(n))
}

直接调用试试看,并不会报错

greet.say("World")

将上面的代码整合在一起

package main

import "fmt"

// Greeting function types
type Greeting func(name string) string

func (g Greeting) say(n string) {
    fmt.Println(g(n))
}

func english(name string) string {
    return "Hello, " + name
}

func main() {
    greet := Greeting(english)
    greet("hello")
    greet.say("World")
}
// output: Hello, World

标签:function,name,Greeting,func,type,string
来源: https://blog.csdn.net/jianlee1991/article/details/120304578