# Is it possible to create a function that only receives structs with a specific type of fields?

**URL:** <https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868>\
**Category:** Getting Help\
**Created:** [August 30, 2023, 8:18pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868 "2023-08-30T20:18:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willy](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/willy/32/11447_2.png) [@Willy](https://forum.golangbridge.org/u/Willy)\
**Post date:** [August 30, 2023, 8:18pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/1 "2023-08-30T20:18:05Z")

</div>

I have this function:

```auto
 func Action(dataStruct any) {
	[...]
}

```

I would like **dataStruct** to have only fields of this group of types `[bool, string, int]`

That means this struct is valid:

```auto
type CustomCase struct {
	Name string
	Cellphone int
	Alive bool
	Native bool
}

```

But this one is not because Names is a slice of strings:

```auto
type CustomCase struct {
	Names []string
	Cellphone int
	Alive bool
	Native bool
}

```

Currently I have it solved with reflect and checking each field, but if what I ask is possible then it would be great because if I code wrong I would have an error at compile time and not at run time how I am currently working.

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [August 30, 2023, 8:32pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/2 "2023-08-30T20:32:13Z")

</div>

If you write/generate a whole bunch of boilerplate for structs with every number of fields you need, with every struct field name you need, you can do it:

```auto
package main

type scalar interface {
	bool | int | string
}

type struct1[T0 scalar] interface {
	~struct{ a T0 } // note that field has to be named "a."
	// I can't get it to work without specifying the name.
}

type struct2[T0, T1 scalar] interface {
	~struct {
		a T0 // field must be named "a"
		b T1 // must be named "b"
	}
}

// you have to define a new struct3, struct4, etc.
// for every struct with every number of fields and every field name

type structs[T0, T1 scalar] interface {
	struct1[T0] | struct2[T0, T1]
	// you have to add a new Tn and structn[...] for every struct
}

func action[T structs[T0, T1], T0, T1 scalar](v T) {}

type OK0 struct{ a int }
type OK1 struct {
	a int
	b string
}

func main() {
	action[OK0, int, int](OK0{})
	action[OK1, int, string](OK1{})
}

```

but this seems completely impractical.

I would instead recommend using the `go/*` package(s) to scan through your source code and identify any structs that don’t meet that criteria.

---

<div class="post-metadata">

**Author:** ![Willy](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/willy/32/11447_2.png) [@Willy](https://forum.golangbridge.org/u/Willy)\
**Post date:** [August 30, 2023, 9:06pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/3 "2023-08-30T21:06:28Z")

</div>

This is not the solution I am looking for because the names are dynamic.

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [August 30, 2023, 9:12pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/4 "2023-08-30T21:12:53Z")

</div>

I figured. Like I said, It’s impractical. In that case, you could write your own “linter” to check for and detect this before you compile, but the Go compiler itself can’t check it.

---

<div class="post-metadata">

**Author:** ![lemarkar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lemarkar/32/12652_2.png) [@lemarkar](https://forum.golangbridge.org/u/lemarkar)\
**Post date:** [September 11, 2023, 12:21pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/5 "2023-09-11T12:21:48Z")

</div>

Have you tried type assertion? Since you have the base struct hardcode (I guess), you can try and make a type assertion in Action and return if the struct is not the one you want.

---

<div class="post-metadata">

**Author:** ![Willy](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/willy/32/11447_2.png) [@Willy](https://forum.golangbridge.org/u/Willy)\
**Post date:** [September 13, 2023, 1:12am UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/6 "2023-09-13T01:12:09Z")

</div>

No, because a struct can repeat several times the same type of field, it can have 3 bools and be valid, a type assertion must be exact and cannot be dynamized as already indicated.

---

<div class="post-metadata">

**Author:** ![lemarkar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lemarkar/32/12652_2.png) [@lemarkar](https://forum.golangbridge.org/u/lemarkar)\
**Post date:** [September 13, 2023, 9:44am UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/8 "2023-09-13T09:44:05Z")

</div>

Oh, sorry, I finally got it. That the structs are also have dynamically changing fields. Didn’t think about it at first

---

<div class="post-metadata">

**Author:** ![mje](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/mje/32/10284_2.png) [@mje](https://forum.golangbridge.org/u/mje)\
**Post date:** [September 13, 2023, 2:55pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/9 "2023-09-13T14:55:27Z")

</div>

> [@lemarkar](#):
>
> structs are also have dynamically changing fields

No, Go is not like, say, Javascript. A struct type has a set of fields fixed at compile time.

---

<div class="post-metadata">

**Author:** ![lemarkar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lemarkar/32/12652_2.png) [@lemarkar](https://forum.golangbridge.org/u/lemarkar)\
**Post date:** [September 13, 2023, 3:04pm UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/10 "2023-09-13T15:04:38Z")

</div>

I know. That was a comment regarding the discussion

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [September 17, 2023, 1:26am UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/11 "2023-09-17T01:26:25Z")

</div>

I think you would need to use reflection to inspect the fields inside the passed structure…

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [December 16, 2023, 1:27am UTC](https://forum.golangbridge.org/t/is-it-possible-to-create-a-function-that-only-receives-structs-with-a-specific-type-of-fields/32868/12 "2023-12-16T01:27:25Z")

</div>

Closed on December 16, 2023, 1:27am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
