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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:23:40+00:00 2026-05-28T07:23:40+00:00

I am looking for help understanding how to access struct fields that are inside

  • 0

I am looking for help understanding how to access struct fields that are inside a container.vector.Vector.

The following code:

package main

import "fmt"
import "container/vector"

func main() {
    type Hdr struct {
        H string
    }
    type Blk struct {
        B string
    }

    a := new(vector.Vector)

    a.Push(Hdr{"Header_1"})
    a.Push(Blk{"Block_1"})

    for i := 0; i < a.Len(); i++ {
        fmt.Printf("a.At(%d) == %+v\n", i, a.At(i))
        x := a.At(i)
        fmt.Printf("%+v\n", x.H)
    }
}

Produces the error prog.go:22: x.H undefined (type interface { } has no field or method H)

removing lines 21 and 22 produces:

a.At(0) == {H:Header_1}
a.At(1) == {B:Block_1}

So, how exactly does one access ‘H’ or ‘B’? It seems like I need to convert those interfaces to structs, but… I dunno. I’m at a loss.

Thanks for any help.

  • 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-28T07:23:41+00:00Added an answer on May 28, 2026 at 7:23 am

    Use a Go type switch or type assertion to distinguish between the Hdr and Blk types. For example,

    package main
    
    import (
        "fmt"
        "container/vector"
    )
    
    func main() {
        type Hdr struct {
            H string
        }
        type Blk struct {
            B string
        }
    
        a := new(vector.Vector)
    
        a.Push(Hdr{"Header_1"})
        a.Push(Blk{"Block_1"})
    
        for i := 0; i < a.Len(); i++ {
            fmt.Printf("a.At(%d) == %+v\n", i, a.At(i))
            x := a.At(i)
            switch x := x.(type) {
            case Hdr:
                fmt.Printf("%+v\n", x.H)
            case Blk:
                fmt.Printf("%+v\n", x.B)
            }
        }
    }
    

    However, effective the weekly.2011-10-18 release:

    The container/vector package has been deleted. Slices are better:
    SliceTricks.

    Therefore, for the latest releases,

    package main
    
    import "fmt"
    
    func main() {
        type Hdr struct {
            H string
        }
        type Blk struct {
            B string
        }
    
        var a []interface{}
    
        a = append(a, Hdr{"Header_1"})
        a = append(a, Blk{"Block_1"})
    
        for i := 0; i < len(a); i++ {
            fmt.Printf("a[%d]) == %+v\n", i, a[i])
            x := a[i]
            switch x := x.(type) {
            case Hdr:
                fmt.Printf("%+v\n", x.H)
            case Blk:
                fmt.Printf("%+v\n", x.B)
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for help in determining if the class model that I am
I'm looking for help to create a query, to solve the following problem: Let's
I'm looking for some help understanding how to generate pages from a database to
I need help understanding how to implement this part of PHP code in Ruby.
Probably a common issue, but I'm looking for help in both fixing and understanding
To help further my understanding of WSGI I'm looking for a diagram which explains
I was looking at the assembler output of my code and need help with
I was looking at C++ STL vector template code to understand exactly how it
I'm looking for help setting a new default property value for an inherited control
I'm looking for help getting started working programmatically with audio. Specifically, the platform I'm

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.