惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
T
The Blog of Author Tim Ferriss
量子位
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
小众软件
小众软件
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
美团技术团队
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Inheritance in Go language
Ashish Bhatia · 2023-03-05 · via ashishb.net

Go language does not have the concept of a class directly. It, however, has a concept of an interface as well as a struct. I’ll illustrate how this can be used to build most of the inheritance constructs that a language like Java or C++ offers.

Inheritance

I’ll use the cliché example of a Rectangle class and a Square class that inherits from it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import "errors"

// Rectangle encapsulates a immutable rectangle
type Rectangle struct {
  width  int32
  height int32
}

// NewRectangle is the constructor of Rectangle
func NewRectangle(width int32, height int32) (*Rectangle, error) {
  if width <= 0 {
    return nil, errors.New("width must be > 0")
  }
  if height <= 0 {
    return nil, errors.New("height must be > 0")
  }
  return &Rectangle{width, height}, nil
}

// Width returns the width
// Guaranteed to be a positive number
func (r Rectangle) Width() int32 {
  return r.width
}

// Height returns the height
// Guaranteed to be a positive number
func (r Rectangle) Height() int32 {
  return r.height
}

func (r Rectangle) Area() int64 {
  return int64(r.width) * int64(r.height)
}

And the cliché Square class that inherits from it

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

// Square struct embeds Rectangle struct
// It gets all methods like Width() and Height() of the parent class
// for free
type Square struct {
  Rectangle
}

// NewSquare is the constructor for the Square1 class
func NewSquare(side int32) (*Square, error) {
  rect, err := NewRectangle(side, side)
  if err != nil {
    return nil, err
  }
  return &Square{*rect}, nil
}

Note: This all works fine, if the parent Rectangle class is immutable, however, if it is mutable, then it is better to use composition. However, that’s not a language-specific issue, so, worth discussing in a separate blog post about inheritance vs composition.

Implementation

Go supports interfaces as well. But unlike Java or C++, no explicit declaration is required.

For example, consider a Shape interface

1
2
3
4
5
6
package main

type Shape interface {
 // Area returns the area of the shape
 Area() int64
}

Now, if one adds the following code to the Rectangle struct then both Rectangle and Square will be implementing the shape interface.

1
2
3
4
// Area returns the area of the Rectangle
func (r Rectangle) Area() int64 {
    return int64(r.width) * int64(r.height)
}

The complete example can be seen at https://go.dev/play/p/exbTybm0GGK