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 7643029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:20:37+00:00 2026-05-31T09:20:37+00:00

I can’t figure out a clean way to implement an algorithm that will work

  • 0

I can’t figure out a clean way to implement an algorithm that will work on any type.

The following code will produce errors trying to convert a string or a typed slice into interfaces, and you can’t compare interface{} objects: invalid operation: result[0] > result[n - 1] (operator > not defined on interface)

func main() {
    c := Algo("abc")
    //...
    c := Algo([3]int{1,2,3})
    //...
}

func Algo(list []interface{}) chan []interface{} {
    n := len(list)
    out := make(chan []interface{})
    go func () {
        for i := 0; i < n; i++ {
            result := make([]interface{}, n)
            copy(result, list)
            // an actually useful algorithm goes here:
            if (result[0] > result[n-1]) {
                result[0], result[n-1] = result[n-1], result[0]
            }
            out <- result
        }
        close(out)
    }()
    return out
}

Although it’s a pain (I think it should be automatic), I can manually box and unbox typed slices into interface{}s, the real problem above is the comparison. And it just keeps getting more and more kludgy.

a := [3]int{1,2,3}
b := make([]interface{}, len(a))
for i, _ := range a {
    b[i] = a[i]
}

I’ve even thought of using vector.Vector, but so many people say never to use them.

So should I just implement the same algorithm for int slices and strings? What about slices of myObject? I can make an interface with a custom comparison func, but then how do I make it work with standard types?

  • 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-05-31T09:20:39+00:00Added an answer on May 31, 2026 at 9:20 am

    You can do this in Go using interfaces. A function that takes an interface type is generic in the sense that it doesn’t care about the data representation of the underlying concrete type. It does everything through method calls.

    To make a generic version of your algorithm then, you have to identify all of the capabilities that the algorithm requires of the data objects and you have to define methods that abstract these capabilities. The abstract method signatures become method sets of interfaces.

    To make a type compatible with this kind of generic algorithm, you define methods on the type to satisfy the interface of the algorithm parameter.

    I’ll take your example code and show one way to do this. Most of the required capabilities happen to be covered by sort.Interface so I chose to embed it. Only one other capability is needed, one to make a copy of the data.

    type algoContainer interface {
        sort.Interface
        Copy() algoContainer
    }
    

    Below is a complete working program made from your example code.

    package main
    
    import (
        "fmt"
        "sort"
    )
    
    func main() {
        s1 := sortableString("abc")
        c1 := Algo(s1)
        fmt.Println(s1, <-c1)
    
        s2 := sortable3Ints([3]int{1,2,3})
        c2 := Algo(&s2)
        fmt.Println(s2, <-c2)
    }
    
    type algoContainer interface {
        sort.Interface
        Copy() algoContainer
    }
    
    type sortableString []byte
    func (s sortableString) Len() int { return len(s) }
    func (s sortableString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    func (s sortableString) Less(i, j int) bool { return s[i] < s[j] }
    func (s sortableString) Copy() algoContainer {
       return append(sortableString{}, s...)
    }
    func (s sortableString) String() string { return string(s) }
    
    type sortable3Ints [3]int
    func (sortable3Ints) Len() int { return 3 }
    func (s *sortable3Ints) Swap(i, j int) {
       (*s)[i], (*s)[j] = (*s)[j], (*s)[i]
    }
    func (s sortable3Ints) Less(i, j int) bool { return s[i] < s[j] }
    func (s sortable3Ints) Copy() algoContainer { c := s; return &c }
    
    func Algo(list algoContainer) chan algoContainer {
        n := list.Len()
        out := make(chan algoContainer)
        go func () {
            for i := 0; i < n; i++ {
                result := list.Copy()
                // actually useful:
                if result.Less(n-1, 0) {
                    result.Swap(n-1, 0)
                }
                out <- result
            }
            close(out)
        }()
        return out
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can't work out a way to make an array of buttons in android. This
can any one tell me how can change this java code into objective c.is
Can someone please help me figure out why my accordion script at http://www.mincovlaw.com/services/copyright gets
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can I set a maximum fps? Is there any way to limit fps in
Can anyone help me trying to find out why this doesn't work. The brushes
Can i get the source code for a WAMP stack installer somewhere? Any help
Can we do following in SQL Server 2005 query with convert function. MaxRegID =
Can multiple objects be linked (bound) to one name in RMI ? If that

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.