How to define dynamic "type struct" in golang?
<p>Here is Playground link <a href="https://play.golang.org/p/qMKxqrOcc2" rel="nofollow noreferrer">https://play.golang.org/p/qMKxqrOcc2</a>. Problem is similar to one that is on Playground.</p>

<p>Let's say I have a condition and need to do this:</p>

<pre><code>if modelName == "a"{
    model = models.A
} 
else{
    model = models.B
}
</code></pre>

<p>where <code>A</code> and <code>B</code> are some models:</p>

<pre><code>type A struct{
    filed1 string
    field2 string
    //etc

}
</code></pre>

<p>and model <code>B</code> is</p>

<pre><code>type B struct{
    filed1 string
    field2 string
    //etc

}
</code></pre>

<p>Fields in <code>A</code> and <code>B</code> has some same fields but mostly they reflect database table (document) and they are of same type (type struct).</p>

<p>When I say in front of all that:</p>

<pre><code>var model interface{}
</code></pre>

<p>I got error:</p>

<pre><code>type models.A is not an expression 
</code></pre>

<p>I am doing this to avoid code redundancy in code if you are asking why.</p>

<p>Question is similar to this: <a href="https://stackoverflow.com/questions/35657362/how-to-return-dynamic-type-struct-in-golang">How to return dynamic type struct in Golang?</a></p>

<p><strong>Here is update for code:</strong></p>

<pre><code>b := c.mainHelper.GetModelBy("id", id, modelName).(map[string]interface{})
mapstructure.Decode(b, &amp;model)

if modelName == "a"{
    model.Photos = []string{"ph1","ph2"}
}
if modelName == "b"{
    model.Docs = []string{"doc1","doc2"}
}

c.mainHelper.UpdateModel(product, id, modelName)
</code></pre>

<p>I know this is stupid and probably is impossible to do but is there and way to do this:</p>

<pre><code>var model models.modelName --&gt; somehow to concat modelName to this models?
</code></pre>

<p><strong>HERE IS NEW UPDATE</strong></p>

<p>I have two models Post and Product. Both of them has Photos field.</p>

<pre><code>type Post struct{

    Photos []string
    //etc
}

type Product {

    Photos []string
    //
}
</code></pre>

<p>Now I need one function that will say this:</p>

<pre><code>func () RemovePhotos(id string, modelName string){

//if modelName=="post"
    //get model post with id

//if modelName=="product"
    //get model product with id

//set model.Photos = []string
//update model in db
}
</code></pre>

<p>I can understand that I can not assign type but how to use this one function to remove data from differnt types? As far as I can see code redundancy will look like this:</p>

<pre><code>func () RemovePhotos(id string, modelName string) return bool{

    if modelName == "post"{

      var model models.Post
      modelWithdata := getModelWithId.(*model)
      modelWithdata.Photos = []string
      //update model in db here
    } 
    if modelName == "product"{
      var model models.Product
      modelWithdata := getModelWithId.(*model)
      modelWithdata.Photos = []string
      //update model in db here
    }

    //it does not matter what I return this is just redundancy example
    return true

}
</code></pre>

<p>As you can only difference is var model <code>models.Post/var model models.Product</code>.
This is redundancy in code and it looks ugly but if there is no way around this then ok, i will have this one completed with redundancy.</p>