Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:22:19+00:00 2026-06-06T21:22:19+00:00

Since Go uses an composition system instead of (multiple) inheritance, I’m just wondering about

  • 0

Since Go uses an composition system instead of (multiple) inheritance, I’m just wondering about these 3 code snippets. Go says they force the programmer to use composition.

A) should be (almost) correct Go-Code,
B) pseudo
C) pseudo

Imho the result will always be the same on all three codes, beside the fact, that B) and C) can be used for even more stuff and A) forces you to stick to composition?

Even if you assume B) to not have the sort-method inside of the class but – lets say global like A) doesn’t make a real difference oO

A) Go code:

interface Sort
    Len()
    Less(i, j int) bool
    Swap(i, j int)

func (qs *Sort) sort()
    doTheSorting

type MyData struct {
    var value int
}

func (s *MyData) Len() { ... }
func (s *MyData) Less(i, j int) bool { ... }
func (s *MyData) Swap(i, j int) { ... }

B) Looks like Inheritance but can imho be seen as embedded, according to how the compiler works.

class Sort

    public sort() { ... }

    abstract Len()
    abstract Less(i, j int) bool
    abstract Swap(i, j int)

C)

interface SortInterface
    void Len()
    bool Less(i, j int)
    void Swap(i, j int)

class Sort implements SortInterface
    public sort() { ... }

Usage B and C:

class MyClass **embed** Sort

    int value

    void Len() { ... }
    bool Less(i, j int) { ... }
    void Swap(i, j int) { ... }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:22:21+00:00Added an answer on June 6, 2026 at 9:22 pm

    No this is not how go works. Here is an example (pulled from the standard library) of a type that can be sorted.

    type IntSlice []int
    
    func (p IntSlice) Len() int           { return len(p) }
    func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
    func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
    

    This implements the interface:

    // Name of interface changed for clarity
    type Sort interface {
        Len() int
        Less(i, j int) bool
        Swap(i, j int)
    }
    

    A type which implement the Sort interface does not get a new method. You can not assign a method to an interface such as in your example func (qs *Sort) sort() {...}.

    However, it is allowed to be passed to functions and methods expecting a variable of type Sort. Because of this, I am able to call sort.Sort(myIntSlice) and it will then be sorted.

    Here is an example function which takes any parameter that implements the Sort interface:

    func IsSorted(data Sort) bool {
        n := data.Len()
        for i := n - 1; i > 0; i-- {
            if data.Less(i, i-1) {
                return false
            }
        }
        return true
    }
    

    In IsSorted, the function has no idea what the true type of data is. It could be IntSlice or anything else. What it does know is that whatever parameter you gave it implements the methods in the Sort interface.

    I do not seem to be able to figure out the question you asked however. Also, psuedo code is very difficult to understand. Using another language such as java would have been better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to upload about 30M for my app since it uses a lot
Since enumeration uses integers, what other structure can I use to give me enum-like
When we can get good speed with OpenGL since it uses texture memory and
I know SysCache uses ASP caching under the hood, but since I'm not aware
I have a spring application that uses an embedded Jetty instance. Since I am
The docs for Camelot say that it uses Elixir models. Since SQLAlchemy has included
since IE won't render XHTML as XHTML, but treat it as HTML instead, when
Since C# uses Garbage Collection. When is it necessary to use .Dispose to free
I have an application that is installed per-machine (since it uses a service). One
We are using Google Analytics for our site, but since it uses client script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.