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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:54:12+00:00 2026-06-13T22:54:12+00:00

I want to write a Join function that takes arbitrary objects with a String()

  • 0

I want to write a Join function that takes arbitrary objects with a String() method:

package main

import (
    "fmt"
    "strings"
)

type myint int

func (i myint) String() string {
    return fmt.Sprintf("%d", i)
}

func main() {
    parts := []myint{myint(1), myint(5), myint(6)}
    fmt.Println(Join(parts, ", "))
}

func Join(parts []fmt.Stringer, sep string) string {
    stringParts := make([]string, len(parts))
    for i, part := range(parts) {
        stringParts [i] = part.String()
    }
    return strings.Join(stringParts , sep)
}

http://play.golang.org/p/EhkbyibzHw

[]myint cannot be passed to Join, because it is not of type fmt.Stringer, so I have to write:

parts := []fmt.Stringer{myint(1), myint(5), myint(6)}

But what if I need parts for another operation where the values have to be ints? Should I cast a slice of myint to a slice of Stringer then?

  • 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-13T22:54:13+00:00Added an answer on June 13, 2026 at 10:54 pm

    Q: Should I cast a slice of myint to a slice of Stringer then?

    There are no casts in Go – only conversions. But conversion from a slice of concrete, non interface type to a slice of interface type is not possible, except by doing it explicitly in a loop. The reason is that, as for example in your case, the []myint backing array has a different memory layout than []fmt.Stringer has. (Related FAQ)

    On a side note, you don’t have to write:

    parts := []myint{myint(1), myint(5), myint(6)}
    

    This should work identically:

    parts := []myint{1, 5, 6}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Powershell function that takes an array argument. I want
Trying to write a for function that takes two strings and returns the characters
I have a function that I want to write and cannot work out how
i just join phonegap, i want a built test application. i write some code
I want to write a program in Prolog that confirms if a b-tree of
I'm trying to write a function that conditionaly prunes out data based on parameters
I'm trying to write a SQL Statement that should function like the below Linq
The following code is an attempt to write a variadic function that acts like
I need to write a function that rounds time in one column, called StartTime,
The problem is in following: I want to write a short program that creates

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.