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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:30:10+00:00 2026-06-18T03:30:10+00:00

I have a struct which represents an object in a database, something like: type

  • 0

I have a struct which represents an object in a database, something like:

type Object struct {
    Id string
    Field1 string
    Field2 int
}

And I’d like to have a function that updates the specific field in the database whenever the field is modified, something along these lines:

func (self *Object) SetField1(value string) {
    self.Field1 = value
    database.Update(self.Id, "Field1", self.Field1) // pseudocode
}

Is there a way to replace the "Field1" hard-coded string such that my code is resistant to future changes in the struct field ordering and naming?

I’ve poked around the reflect package, and it would be nice to be able to get the StructField that represents the field I’m working with, but it seems to require either the name of the field via hard-coded string, or the field’s index in the struct (which is subject to change).

  • 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-18T03:30:12+00:00Added an answer on June 18, 2026 at 3:30 am

    You could validate your string by adding a method something like this:

    func (self *Object) verify(field string) string {
        if _, ok := reflect.TypeOf(*self).FieldByName(field); ok {
            return field
        }
        panic("Invalid field name")
    }
    

    And then use it when passing the string to the database update

    func (self *Object) SetField1(value string) {
        self.Field1 = value
        database.Update(self.Id, self.verify("Field1"), self.Field1) // pseudocode
    }
    

    But I would think that if you’re willing to use reflection, that you’d be better off just making a generic setField method that accepts the field as a string, and the value as a interface{}, checks the field and value, sets the value and updates the database.

    This way everything is done using the string, so it’ll either work or panic, and you don’t need to remember to use the .verify() method.

    Somthing like:

    func (self *Object) SetField(field string, value interface{}) {
        // verify field and value using reflection
        // set the value using reflection
        database.Update(self.Id, field, self.Field1)
    
    }
    

    Though I don’t think this’ll work on unexported fields.

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

Sidebar

Related Questions

I have this type which is basically a struct { int x,y,z; } that
For example, I have a struct which is something like this: struct Test {
I have a struct that looks like this, struct Foo { int a; };
I have a type which I consider use it as struct. It represents single
I have a struct which corresponds to a register in hardware. typedef unsigned int
I have a struct which has several arrays within it. The arrays have type
I have a function which takes a custom struct as the argument. how can
I have in my program a struct type called Square which is used to
I have a struct that represents a vertex. It has x, y and z
I have a struct that represents a vertex. It has x, y and z

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.