首页 > 其他分享 >Go - Composing Structs from Other Structs

Go - Composing Structs from Other Structs

时间:2023-10-07 15:57:52浏览次数:43  
标签:struct Person fields mgr Manager Other Go data Structs

Problem: You want a struct that has data and methods of another struct.

 

Solution: Embed an unnamed struct within another struct. The outer struct will gain the data and methods of the inner struct.

 

Inheritance is not the only mechanism you can use for these purposes. Another popular mechanism is composition, which allows you to assemble your models from other models instead of inheriting from a superclass. Go implements composition by embedding an unnamed struct within another struct.

Structs can contain named data fields including structs. However, structs can also contain unnamed data fields, including structs. Whenever a struct has an unnamed data field that is a struct, the data fields within that inner struct are promoted to the outer struct:

type   Person   struct   { 
      Id      int 
      Name    string 
      Email   string 
} 

type   Manager   struct   { 
      Person 
} 

mgr   :=   Manager {} 
mgr . Id   =   2 
mgr . Name   =   "Wong  Fei  Hung" 
mgr . Email   =   "[email protected]"

In this example, Manager has a single unnamed data field that is of the type Person . The data fields of Person are promoted to mgr and you can access them directly, without referencing Person at all.

If you try to put mgr through to fmt.Println , you will see this:

{{2  Wong  Fei  Hung  [email protected]}}

Notice the double braces {{}} used. This tells you that there is an inner struct instance.

The same rules for exported fields apply here as well — capitalized variable names indicate the fields are exported, while lowercase variable names indicate they are not.

How about methods? They are also promoted to the outer struct:

func   ( person   Person )   Work ()   { 
      fmt . Print ( "Working  hard  ...  " ) 
} 

mgr . Work ()

You can call the Work method directly on mgr because the methods associated with Person are promoted to be available on Manager as well.

This also means any interfaces that Person satisfies, Manager also satisfies:

type   Worker   interface   { 
      Work () 
} 

func   Pay ( worker   Worker )   { 
      worker . Work () 
      fmt . Println ( "and  getting  paid!" ) 
} 

Pay ( mgr )

Since Person satisfies the Worker interface, this means Manager also satisfies the Worker interface and therefore you can pass an instance of Manager to the Pay function.

 

标签:struct,Person,fields,mgr,Manager,Other,Go,data,Structs
From: https://www.cnblogs.com/zhangzhihui/p/17746501.html

相关文章

  • idea报错:Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (d
    idea版本:​​2020.3​​idea报错:在查阅了资料以后发现是​​IDEA2020​​的兼容问题 Failedtoexecutegoalorg.codehaus.mojo:exec-maven-plugin:3.0.0:exec(default-cli)onprojectsms:Commandexecutionfailed.解决方法:1、测试不要用​​main​​方法测试,使用​​......
  • Go - Creating One - Time Structs
     person:=struct{IdintNamestringEmailstring}{1,"ChangSauSheong","[email protected]"} person=struct{IdintNamestringEmails......
  • 2023-10-07:用go语言,给定n个二维坐标,表示在二维平面的n个点, 坐标为double类型,精度最多
    2023-10-07:用go语言,给定n个二维坐标,表示在二维平面的n个点,坐标为double类型,精度最多小数点后两位,希望在二维平面上画一个圆,圈住其中的k个点,其他的n-k个点都要在圆外。返回一个圆心和半径,表示哪个圆可以圈住其中的k个点。坐标和半径都是double类型,最多保留小数点后两位。下面......
  • 代码源:a-good string(CF1385D,分支)
    传送点击查看代码#include<bits/stdc++.h>usingnamespacestd;chars[131080];int_solve(intL,intR,charx){ if(L==R)returns[L]!=x; intM=L+(R-L)/2; intt1=0,t2=0; for(inti=L;i<=M;++i)if(s[i]!=x)t1++; for(inti=M+1;i<=R;++i)if(s[i]!=x)......
  • AIGC革新,将文字或者LOGO融入AI视频基于PIKA-labs(Python3.10)
    很多平台都会禁止用户使用带有网址或者二维码的头像以及文章配图,这样可以有效的防止用户的一些“导流”行为。当然,头像、文章或者视频现在都是AI来审,毕竟现在人工的成本实在太高,但是如果我们把文字元素直接融入图像或者视频之中,如此一来,AI也会很难识别出一些“导流”的元素。本次......
  • vulnhub - Aragog - writeup
    信息收集目标开放了80、22端口。root@Locklytemp/tmp»arp-scan-Ieth1-lInterface:eth1,type:EN10MB,MAC:00:0c:29:fa:3d:23,IPv4:192.168.56.106Startingarp-scan1.10.0with256hosts(https://githu......
  • Go - Creating Struct Instances
    Problem: Youwanttocreateaninstanceofastruct.Solution: Createastructinstancedirectlyusingthenameofthestruct,orapointertoastructinstanceusingthenewkeyword. Therearetwowaystocreateaninstanceofastruct.Thefirstist......
  • Strimzi Kafka Bridge(桥接)实战之三:自制sdk(golang版本)
    欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos本篇概览本文是《StrimziKafkaBridge(桥接)实战》的第三篇,前文咱们掌握了StrimziKafkaBridge的基本功能:基于http提供各种kafka消息的服务此刻,如果想通过http接口调......
  • WIN11 安装 SQL Server 2019,SQLSERVER2022, MYSQL 8.0 ,Docker,Mongodb失败故障分析
    最近研究数据库性能调优遇到各种数据库各种装不上,不知道熬了多少根软白沙,熬了多少颗张三疯,问了多少AI,查了多少网页,熬了两天,终于搞明白了一件事:那就是WIN11ONARM(因为拿的是MACPROM2做.NET平台开发安装)SQLSERVER2019,SQLSERVER2022,MYSQL8.0,Docker,Mongodb失败故障分析,最终极......
  • 基于goravel的CMS,企业官网通用golang后台管理系统
    2023年9月11日10:47:00仓库地址:https://gitee.com/open-php/zx-goravel-websiteCMS,企业官网通用PHP后台管理系统框架介绍GoravelSCUI后端开发组件go1.20Goravel1.13数据库sql(使用最新日期文件)goravel\doc\sql_bakmysql8.0前端开发组件scui1.6.9nodev14.21.......