Golang entry struct inheritance, nesting

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport "FMT" type jocongmin struct{name stringhome stringwant String}func (J *jocongmin) Sayname () string{
  
   //here defines the method is to expand the method, is the inheritance of jocongmin this struct, that is, the struct more than a say. method, you can call return by jocongmin this struct instance "My name is" +j.name}func (J *jocongmin) Sayhome () String{return "My Home In" +j.home}func (J * jocongmin) saywant () String{return "My favorite is" +j.want}func main () {var jocongmin jocongminjocongmin.name= "Week smart" jocongmin. Home= "Fujian province" Jocongmin. Want= "Money" FMT. Println (jocongmin. Sayname ())//called the method FMT for the struct. Println (jocongmin. Sayhome ()) fmt. Println (jocongmin. Saywant ())}
  

  Nesting of 2.struct

Package Mainimport "Encoding/json" import "FMT" import "StrConv" Import ("Github.com/drone/routes" "net/http") type resinfo struct {//defines a struct, and what sub-objects in the struct have data yeardatastructmsg string}type yeardatastruct struct {mouthall []mo Uthstruct//Defines an object of type array, and then the element type of this array is some kind of structsum detailstructaverage detailstructquarter []quarterstruct}type Deta ilstruct struct {One inttwo intthree int}type quarterstruct struct {detailstruct//can nest composite other types of structs, inheriting other structs Sub-object Quarternum int}type mouthstruct struct {Mouth intpartmentitem []itemarrstruct}type itemarrstruct struct {PartMe NT Stringdetailstruct}func Cross (w http. Responsewriter) {W.header (). Set ("Access-control-allow-origin", "*")//Allow access to all domain W. Header (). ADD ("Access-control-allow-headers", "Content-type")//header the type W. Header (). Set ("Content-type", "Application/json")//Return data format is json}func main () {FMT. Println ("Starting Web Service ...") var mux *routes. Routemux = routes. New () Mux. Get ("/test", Datasend)//http. Handle ("/", MUX) http. Listenandserve (": 8088", MUX) fmt. PRINTLN ("service stopped")}func Datasend (w http. Responsewriter, R *http. Request) {var data yeardatastructfor I: = 1; i <; i++ {var partment []itemarrstructfor t: = 1; t < 4; t++ {Partme NT = Append (partment, itemarrstruct{partment: "" + StrConv.  Itoa (t) + "ABC", detailstruct:detailstruct{//Inherit the assignment method of sub-objects of other structs, such as one, and partment are the same level one:45,two:23,three: 3432,},})}data. Mouthall = append (data. Mouthall, Mouthstruct{mouth:i,partmentitem:partment,})}for I: = 1; I <= 4; i++ {data. Quarter = append (data. Quarter, Quarterstruct{detailstruct:detailstruct{one:4324,two:23423,three:4324234,},quarternum:i,})}data. Sum = Detailstruct{one:4546,two:454,three:454,}data. Average = Detailstruct{one:4546,two:454,three:465465,}var Res resinfores.data = datares. MSG = "Right" ResJson, _: = json. Marshal (res) FMT. Fprintln (W, String (ResJson))}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.