Can declare constant's into struct?


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #1

hi everyone

how can use constants into struct ?

type game struct {
constants.GAME_ID int
}

I cant’s declare constant’s into struct plz help.

thanks in advance


(Anders Kristensen) #2

Go does not support constants in structs.


(Christoph Berger) #3

Let’s assume that constants.GAME_ID has the value 1. Then this syntax

type game struct {
    constants.GAME_ID int
}

would evaluate to

type game struct {
    1 int
}

which is an invalid syntax and also has no semantic meaning.

If you want to assign each game variable a game ID, you can add an unexported field to the struct, as well as a method that returns the ID value. This way, code outside the package can read the ID via the method but cannot access the variable directly and therefore cannot change it.

This code illustrates the concept:

package games

import (
	"fmt"
)

const (
	GAME_ID1 = 12345	
        GAME_ID2 = 67890
)

type Game struct {
	id int
}

func (g Game) Id() int {
	return g.id
}
package main

import "path/to/games"

func main() {
	game1 := games.Game{
		id: GAME_ID1,
	}
	fmt.Println("Game ID:", game1.Id())

        // This will not compile, as id is unexported:
        game1.id = 67890
}

(Playground link with simplified code (no packages))


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #4

very well then do u have solutions for that?


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #5

thanks it’s good but not enough, I think this inflexible struct is not good.
this is worrying :frowning:


(Jakob Borg) #6

It’s not clear to me what you actually want to accomplish. What is your goal? Knowing that, you can probably get some recommendations.


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #8

my constant’s file :

 const (
    C_GAME_ID   = "game_id"
    C_GAME_NAME = "game_name"
    C_THUMB_URL = "thumb_url"
)

My struct:

type GameO struct {
	GameId       string    `json:"game_id,omitempty"`
	GameName     string    `json:"game_name,omitempty"`
	ThumbUrl     string    `json:"thumb_url,omitempty"`
}

and this is Database query Cursor:

...
gameRows := sql.Query("SELECT " + C_GAME_ID + "," + C_GAME_NAME + "," + C_THUMB_URL + " FROM games;")
...
var game_id, game_name, thumb_url
for gameRows.next() {
err = gameRows.Scan( &game_id, &game_name, &thumb_url)
game := structs.GameO {
  GameId: game_id,
  GameName: game_name,
  ThumbUrl: thumb_url
  ...
}
...

I want to replace actual values in above code with their constants


(Jakob Borg) #9

If it’s about having the struct describe which fields to select from, take a look at https://github.com/jmoiron/sqlx for some useful shortcuts on top of the standard library. You might also want to make the select thing a method of the type itself, so the definition is close to the struct definition.

You cannot have struct fields named by some constant in the same code, excerpt by using code generation or (possibly) reflect. Don’t go there until you are very familiar with all aspects of Go and still think it’s a good idea.


(Stratos) #10

You could define custom constant types and implement the Valuer and Scanner interfaces to define how they get stored and retrieved from the database. if you choose this way, you might also have to implement the Marshaler and Unmarshaler interfaces.


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #11

thanks for responses


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #12

That’s right thanks for responses