How to specify the field we wanna use from a struct?
I have a struct composed of multiple fields of same type.
type test struct{
A int
B int
C int
}
I want to apply a function that does the same things to the three fields, but I only wanna do it on one each time.
function something (toto test, cond int) {
if (cond == 1){
// then we will use A for the rest of the function
}else if (cond == 2) {
// then we use B etc....
} ...
for mail, v := range bdd {
if _, ok := someMap[v.A]; !ok { // use v.A or V.B or V.C
delete(bdd, mail)
}
...
}
...
}
The function is really long and I it bothers me to have the code duplicated like 3 times just for one line that changes.
I tried things with the reflect package. I think it's a dangerous idea to go into that.
go struct field reflect
add a comment |
I have a struct composed of multiple fields of same type.
type test struct{
A int
B int
C int
}
I want to apply a function that does the same things to the three fields, but I only wanna do it on one each time.
function something (toto test, cond int) {
if (cond == 1){
// then we will use A for the rest of the function
}else if (cond == 2) {
// then we use B etc....
} ...
for mail, v := range bdd {
if _, ok := someMap[v.A]; !ok { // use v.A or V.B or V.C
delete(bdd, mail)
}
...
}
...
}
The function is really long and I it bothers me to have the code duplicated like 3 times just for one line that changes.
I tried things with the reflect package. I think it's a dangerous idea to go into that.
go struct field reflect
2
Are you sure you should be storing that information as struct fields? Perhaps a map would be more suitable.
– Tim Cooper
Nov 23 '18 at 14:26
@TimCooper Actually i'm using something like a map[email]test so test contains informations related to that email and i have more than 3 fields actually Making one map per fields is a bit too much and not practical I think
– Dylan
Nov 23 '18 at 14:28
2
If you don't want to use reflection you could make the function variadic taking pointers to the fields. e.gsomething(&t.A, &t.B, &t.C)
– mkopriva
Nov 23 '18 at 15:49
add a comment |
I have a struct composed of multiple fields of same type.
type test struct{
A int
B int
C int
}
I want to apply a function that does the same things to the three fields, but I only wanna do it on one each time.
function something (toto test, cond int) {
if (cond == 1){
// then we will use A for the rest of the function
}else if (cond == 2) {
// then we use B etc....
} ...
for mail, v := range bdd {
if _, ok := someMap[v.A]; !ok { // use v.A or V.B or V.C
delete(bdd, mail)
}
...
}
...
}
The function is really long and I it bothers me to have the code duplicated like 3 times just for one line that changes.
I tried things with the reflect package. I think it's a dangerous idea to go into that.
go struct field reflect
I have a struct composed of multiple fields of same type.
type test struct{
A int
B int
C int
}
I want to apply a function that does the same things to the three fields, but I only wanna do it on one each time.
function something (toto test, cond int) {
if (cond == 1){
// then we will use A for the rest of the function
}else if (cond == 2) {
// then we use B etc....
} ...
for mail, v := range bdd {
if _, ok := someMap[v.A]; !ok { // use v.A or V.B or V.C
delete(bdd, mail)
}
...
}
...
}
The function is really long and I it bothers me to have the code duplicated like 3 times just for one line that changes.
I tried things with the reflect package. I think it's a dangerous idea to go into that.
go struct field reflect
go struct field reflect
edited Nov 23 '18 at 14:38
Dylan
asked Nov 23 '18 at 14:25
DylanDylan
306
306
2
Are you sure you should be storing that information as struct fields? Perhaps a map would be more suitable.
– Tim Cooper
Nov 23 '18 at 14:26
@TimCooper Actually i'm using something like a map[email]test so test contains informations related to that email and i have more than 3 fields actually Making one map per fields is a bit too much and not practical I think
– Dylan
Nov 23 '18 at 14:28
2
If you don't want to use reflection you could make the function variadic taking pointers to the fields. e.gsomething(&t.A, &t.B, &t.C)
– mkopriva
Nov 23 '18 at 15:49
add a comment |
2
Are you sure you should be storing that information as struct fields? Perhaps a map would be more suitable.
– Tim Cooper
Nov 23 '18 at 14:26
@TimCooper Actually i'm using something like a map[email]test so test contains informations related to that email and i have more than 3 fields actually Making one map per fields is a bit too much and not practical I think
– Dylan
Nov 23 '18 at 14:28
2
If you don't want to use reflection you could make the function variadic taking pointers to the fields. e.gsomething(&t.A, &t.B, &t.C)
– mkopriva
Nov 23 '18 at 15:49
2
2
Are you sure you should be storing that information as struct fields? Perhaps a map would be more suitable.
– Tim Cooper
Nov 23 '18 at 14:26
Are you sure you should be storing that information as struct fields? Perhaps a map would be more suitable.
– Tim Cooper
Nov 23 '18 at 14:26
@TimCooper Actually i'm using something like a map[email]test so test contains informations related to that email and i have more than 3 fields actually Making one map per fields is a bit too much and not practical I think
– Dylan
Nov 23 '18 at 14:28
@TimCooper Actually i'm using something like a map[email]test so test contains informations related to that email and i have more than 3 fields actually Making one map per fields is a bit too much and not practical I think
– Dylan
Nov 23 '18 at 14:28
2
2
If you don't want to use reflection you could make the function variadic taking pointers to the fields. e.g
something(&t.A, &t.B, &t.C)– mkopriva
Nov 23 '18 at 15:49
If you don't want to use reflection you could make the function variadic taking pointers to the fields. e.g
something(&t.A, &t.B, &t.C)– mkopriva
Nov 23 '18 at 15:49
add a comment |
1 Answer
1
active
oldest
votes
In your situation I'd use map instead of struct, but if struct is really required you can use reflect package.
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
fmt.Printf("%v", v.Field(i).Interface())
}
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448467%2fhow-to-specify-the-field-we-wanna-use-from-a-struct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your situation I'd use map instead of struct, but if struct is really required you can use reflect package.
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
fmt.Printf("%v", v.Field(i).Interface())
}
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
add a comment |
In your situation I'd use map instead of struct, but if struct is really required you can use reflect package.
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
fmt.Printf("%v", v.Field(i).Interface())
}
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
add a comment |
In your situation I'd use map instead of struct, but if struct is really required you can use reflect package.
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
fmt.Printf("%v", v.Field(i).Interface())
}
In your situation I'd use map instead of struct, but if struct is really required you can use reflect package.
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
fmt.Printf("%v", v.Field(i).Interface())
}
answered Nov 23 '18 at 14:38
Alex PliutauAlex Pliutau
14.3k2490131
14.3k2490131
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
add a comment |
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
Yeah that was what I tried to do, but then I realised that in a for loop, we had to call reflect for each iteration. I wanted to set the field I wanna use, just once, at the beginning of the function.
– Dylan
Nov 23 '18 at 14:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448467%2fhow-to-specify-the-field-we-wanna-use-from-a-struct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Are you sure you should be storing that information as struct fields? Perhaps a map would be more suitable.
– Tim Cooper
Nov 23 '18 at 14:26
@TimCooper Actually i'm using something like a map[email]test so test contains informations related to that email and i have more than 3 fields actually Making one map per fields is a bit too much and not practical I think
– Dylan
Nov 23 '18 at 14:28
2
If you don't want to use reflection you could make the function variadic taking pointers to the fields. e.g
something(&t.A, &t.B, &t.C)– mkopriva
Nov 23 '18 at 15:49