首页 > 其他分享 >[Go] Method

[Go] Method

时间:2022-09-06 02:45:39浏览次数:68  
标签:string FirstName describe User Go Method struct

// User
type User struct {
	ID        int
	FirstName string
}

// Method
func (u *User) describe() string {
	desc := fmt.Sprintf("Name %s", u.FirstName)
	return desc
}

// Order:
// 0 1 2 defer panic: NOT 2
func main() {
	user := User{FirstName: "JK", ID: 1}
	fmt.Println(user.describe())
}

describe() is a prop method on User struct

标签:string,FirstName,describe,User,Go,Method,struct
From: https://www.cnblogs.com/Answer1215/p/16660304.html

相关文章

  • [Google] LeetCode 2172 Maximum AND Sum of Array 状态压缩DP
    YouaregivenanintegerarraynumsoflengthnandanintegernumSlotssuchthat2*numSlots>=n.TherearenumSlotsslotsnumberedfrom1tonumSlots.You......
  • [Go] Error
    packagemainimport( "errors" "fmt" "strings")funcshouldGreaterThanTen(numint)error{ ifnum<10{ returnerrors.New("Numberislessthan10") ......
  • [Go] Defer, panic, recover
    packagemainimport( "fmt")funcrecoverFromPanic(){ fmt.Println("defer")}funcdoThings(){ deferrecoverFromPanic() fori:=0;i<5;i++{ fmt......
  • [GO] Pass by reference
    funcchangeName(name*string){ *name=strings.ToUpper(*name)}//CoordinatestypeCoordinatesstruct{ X,Yfloat64}funcmain(){ name:="Elvis" ch......
  • [Go] Pointer
    varnamestring varnamePointer*string//Pointer name="Beyonce" namePointer=&name//AssignaPointer fmt.Println("Name:",name) fmt.Println("Nam......
  • django ORM常见查询关键字与外键字段数据操作
    今日内容表查询数据准备及测试环境搭建1.django自带一个sqllite3小型数据库 该数据库功能非常有限并且针对日期类型的数据兼容性很差2.django切换MySQL数据 针对dja......
  • django框架之字段类型及表查询
    ORM操作数据准备及测试环境搭建1.数据库切换:配置文件settings中配置mysql数据库DATABASES={'default':{'ENGINE':'django.db.backends.mysql',......
  • Django ORM 常用字段和表查询
    表查询数据准备及测试环境搭建sqllite3django自带sqllite3小型数据库该数据库功能非常有限,并且针对日期类型的数据兼容性很差django切换MySQL数据django1.x版本的......
  • django前后端不分离入门
    在django中创建html模板,关联数据,返回给浏览器修改settings.py文件将'DIRS':[]修改为'DIRS':[os.path.join(BASE_DIR,'templates')],项目目录下创建模板文件夹te......
  • Go 执行命令行并实时打印输出
    golang可以获取命令执行的输出结果,但要执行完才能够获取。如果执行的命令是ssh,我们要实时获取,并执行相应的操作呢?见:https://blog.csdn.net/universsky2015/article/detai......