Golang Azərbaycan
  • Info
  • Go Compiler Installations
  • First Go Application
  • Build on Different Platforms
  • Values
  • Variables
  • Assignment Operators
  • Code Grouping Process
  • Type Conversion
  • Comments
  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Constants
  • if/else
  • For
  • Switch
  • Arrays
  • Slices
  • Maps
  • Range
  • Functions
    • Functions
    • Variadic function
    • Closures
    • Anonymous
    • Recursion
  • Empty Identifiers
  • Pointers
  • Strings and Runes
  • Structs
  • Anonymous struct
  • Method
  • Interface
  • Dynamic Variables
  • Generics
  • Struct embedding
  • Errors
  • Goroutines
  • Channel
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Ticker
  • Worker pools
  • WaitGroups
  • Rate Limiting
  • Atomic Counter
  • Sorting
  • Panic
  • Defer
  • String Functions
  • Recover
  • Text Templates
  • Json
  • Database
    • Mysql
    • Postgres
    • Mssql
Powered by GitBook
On this page
Edit on GitHub

Struct embedding

Go dilində struct embedding, bir struct-ın başqa bir struct daxilində yerləşdirilməsi ilə digər struct-ın sahələrinə və metodlarına birbaşa giriş imkanı verir. Bu, təkcə kod təkrarını azaltmır, həm də kompozisiyanı təşviq edir, beləliklə, bir struct-ın başqa bir struct-a miras buraxmadan onun funksionallığını təmin etməyə imkan verir.

package main

import "fmt"

type Person struct {
	Name string
	Age  int
}

func (p Person) SayHello() {
	fmt.Printf("Hello, my name is %s and I'm %d years old\n", p.Name, p.Age)
}

type Employee struct {
	Person
	Company string
}

func main() {
	emp := Employee{
		Person: Person{
			Name: "John",
			Age:  30,
		},
		Company: "ABC Inc.",
	}

	emp.SayHello()
}

Bu nümunədə, Person adlı bir struct təyin edilir və Name və Age adlı iki sahəsi var. Eyni zamanda SayHello adlı bir funksiyası mövcuddur.

Employee adlı bir struct təyin edilir və onun içərisində Person struct-ı yerləşdirilir. Employee-nin əlavə olaraq Company adlı bir sahəsi də var. main funksiyasında, emp adlı bir Employee dəyişəni yaradılır. Bu dəyişənin Person sahəsinə Person tipində bir dəyər atanır. SayHello funksiyası emp dəyişəni üzərindən çağırılır və nəticə ekrana yazdırılır.

Output:

Hello, my name is John and I'm 30 years old

Bu nümunədə, Employee adlı struktur, Person strukturunu daxili olaraq yerləşdirir. Bu, Employee strukturuna Person-un bütün sahələrinə və funksiyalarına birbaşa çıxış imkanı verir. Beləliklə, Employee strukturunu istifadə edərək həm Employee, həm də Person məlumatlarına asanlıqla giriş əldə etmək mümkündür. Bu xüsusiyyət, kod təkrarını azaldır və strukturları daha modul halına gətirir.

PreviousGenericsNextErrors

Last updated 8 months ago