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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:16:15+00:00 2026-06-12T11:16:15+00:00

I have type DocId int func foo(documents []String) { for i := range documents

  • 0

I have

type DocId int

func foo(documents []String) {
    for i := range documents {
        id := DocId(i)
        ...
    }
}

How do I get rid of the explicit conversion line? DocIds are meant to be the type that indexes individual documents.

What I want is more like:

func foo(documents []String) {
    for id := range documents {
        ... // id gets used as the DocId that it damn well *IS*
    }
}

Which gives me ‘invalid operation: … (mismatched types int and DocId)’ when I attempt to use the id from range as a DocId, even though DocId is an 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-12T11:16:17+00:00Added an answer on June 12, 2026 at 11:16 am

    Only untyped constant can be automatically converted in the right type.

    You can find examples where the cast is not needed like in this thread, for untyped constant. From the specs:

    if one operand is an untyped constant and the other operand is not, the constant is converted to the type of the other operand

    But here, range explicitly assign iteration values to the corresponding iteration variables (i int, T a[i]), which makes i and int. (from spec “For statement“)

    For all the other case, an explicit cast is required.
    The “Conversion” section of the specs doesn’t mention an “automatic” type conversion (as opposed to automatic interface conversion, which happens all the time: interface{}).

    This thread adds

    type A int
    type B int
    
    ...
    
    var a A = 5
    var b B = a    // Compiler ERROR!!!!!!
    

    Essentially, since (nearly) all types are unique, even two types with the same underlying representation and primitive operations are unassignable without a cast. […]
    This makes the language type-safe by forcing you to explicitly tell the compiler when you want the conversion to happen.

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

Sidebar

Related Questions

Let's say I have type Person struct { Name string } func (p *Person)
I have this type which is basically a struct { int x,y,z; } that
I have a database table that stores document information (DocID, Name, Type, Owner, etc).
I have two tables in my database Table:Documents Id (int), DocName (nvarchar) ---------------------------- Table:AccessLogs
I have some code that iterates through 'Documents' in couchDB for docid in db:
I have type gedit filename.txt on my command line in Ubuntu, but no Gedit
Ok, so let's say I have a type defined like so: type Foo =
Let's say I have type A, and a derived type B. When I perform
In C# we have Type.FullName and Type.Name for getting the name of a type
I have a struct which has several arrays within it. The arrays have type

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.