How to group variables under a name?

I want to create some constant data in the code which is accessible from other .go files

decl.go

package declarations

type keydata struct {
bufkeyvalue uint8
bufforeignvalue unit8
}

My question is how to assign values to bufkeyvalue and bufforeignvalue ?
I tried the same using const() but when i try to give a name like

const keydata (
bufkeyvalue = 05
bufforeignvalue = 0A
)

I got error.

below does not throw error
const(
bufkeyvalue = 05
bufforeignvalue = 0A
)

but i want to group bufkeyvalue and bufforeignvalue under a parent name

Is there any room for improvements in this declarations?
Thanks

You can’t group constants like that. Just use clear names, perhaps with prefixes or suffixes like

KeyDataBufKeyValue = 0x5
KeyDataBufForeignValue = 0xA

Hi @amelia
First, try to create a working program without constants. This is necessary in order to understand the principle of exporting names starting with a capital letter. Example:
decl/decl.go:

package decl

type Keydata struct {
	Bufkeyvalue uint8
	Bufforeignvalue uint8
}

main.go:

package main

import (
    "fmt"
	"./decl"
)

func main() {
	//var k = decl.Keydata{Bufkeyvalue: 0x5, Bufforeignvalue: 0xA}
	k := decl.Keydata{0x5, 0xA}
	fmt.Println(k.Bufkeyvalue, k.Bufforeignvalue)
}

If I’m not mistaken, golang does not support constants in structs.
You probably need to consider other types of data in your program
but for fun you can apply the workaround.
decl/decl.go:

package decl

type keydata struct {
	bufkeyvalue uint8
	bufforeignvalue uint8
}

func ConstKeydata() keydata {
	return keydata{0x5, 0xA}
}

main.go:

package main

import (
    "fmt"
	"./decl"
	"reflect"
)

func main() {
	k := decl.ConstKeydata()
	val := reflect.ValueOf(&k)
	fmt.Println(val.Elem().FieldByName("bufkeyvalue"),
		val.Elem().FieldByName("bufforeignvalue"))
}

Sorry for my French.

1 Like

Let me include this in my code and update you. Thanks a lot

1 Like

@nezabudka
//Most of the variable I declare should be exported. Hence these variables are in UpperCase
//This the default value that HAS to be assigned to the declared variables.

> import (
>  "encoding/hex"
>  "fmt"
> )
> 
> var baseValues = map[string] int64 {    
>     "DefaultA" :      0x0000010300010002,
>     "DefaultB" :      0x0000010300020002,
>     "DefaultC"  :     11,
> }

When I print the values, I get below output

Map Values : map[DefaultA:138781130753 DefaultB:2220498157569 DefaultC:11]

I did go thru some examples in golang.org and there was mention of decoder/encoder.

I used fmt.Printf("%X",baseValues[“DefaultA”]) and the output I got was 10300010002.

My question is " Is there any direct way to store 16 byte hex value to a variable and passing the same hex value to another variable without converting ?
Thanks

Hi @amelia
All data is stored in variables in binary format only. The compiler understands decimal, octal and hexadecimal immediate values and converts them to binary. In what representation to display them it is already your task.

1 Like

Do you mean that you want to store the string that fmt.Printf prints? If so, then you can use fmt.Sprintf which returns the value as a string instead of printing it to standard output:

hexString := fmt.Sprintf("%X", baseValues["DefaultA"])
2 Likes

Thank you. Yes I want other packages to access the variables (DefaultA…C) with default values as defined inside the map. I tried the command you sent and was able to get the actual value.

   >>               hexString := fmt.Sprintf("%X\n", baseValues["DefaultA"])
                    fmt.Println(hexString)

10300010002

Forgot to check. I have been going thru go tutorial and though they say “map” can take any data type, but no mention of hex

eg

var n = map[string] hex {
}

I see many examples with int/float32 but no mention of hex

I think this is what @nezabudka was talking about: “hex” isn’t a data type; it’s a notation to represent data. The number 4275878552 can be represented in hex as fedcba98 or in binary as 11111110110111001011101010011000, but they’re all the same data type; it’s just in how you show the data.

I don’t mean to split hairs. I’m just mentioning it to see if this is really what you’re looking for ¯\_(ツ)_/¯

If you want to store the hex representations of int64s as strings, then you can do this:

func hexInt64(i int64) string {
    return fmt.Sprintf("%X", i)}
}

var n = map[string]string{
    "DefaultA": hexInt64(0x0000010300010002),
    "DefaultB": hexInt64(0x0000010300020002),
    "DefaultC": hexInt64(11),
}