Access value using key from a struct


(Hemanth Naidu) #1

I want to access value(string) from key(int) which are mapped in a struct. Any leads. I have written a code in javaScript. I want to perform similar in go.
Here is the code in Javascript.

function resetCause(bits) {
    var resetDictionary = {
        '1': 'Watchdog Reset',
        '2': 'Power On Reset',
        '3': 'System Request Reset',
        '4': 'Other Resets',
    };
    return resetDictionary[bits];
}

Here bits is a decimal value (basically 1,2,3,4)which I can pass from part of the code and access the value (string).


(Yamil Bracho) #2

It looks like a map

func resetCause(bits int) (string, bool) {
	resetMap := map[int]string{
		1: "Watchdog Reset",
		2: "Power On Reset",
		3: "System Request Reset",
		4: "Other Resets",
	}

	value, ok := resetMap[bits]
	return value, ok
}

(Hemanth Naidu) #3

Yes, but how do i access the value when I pass the key.
E.g. I want to put the value (string) into a new variable by calling the function. In JavaScript

var reason = resetCause(3)
console.log(reason)

and it returns

"System Request Reset"

I was trying something like this in go

func main() {
      bits := 3
      resetCause(bits) 
}

and it throws error

syntax error: unexpected main, expecting (

(Yamil Bracho) #4

There is no problem is you send a constant o a var as parameter.
This works

func main() {
	bits := 3
	value, ok := resetCause(bits)
	if ok {
		fmt.Println("Value=", value)
	}
}

(Hemanth Naidu) #5

Still same error.


(Yamil Bracho) #6

Please, publish the full code. It seems another problem…
You can also create your version in golang playground and gives us the link…


(Hemanth Naidu) #7

Here is the link!! [https://play.golang.org/p/2kyvWggWNub].


(Andre) #8

fixed version
https://play.golang.org/p/NoR__Y1sfMH


(Hemanth Naidu) #9

Thanks @cinematik. What was causing this error?


(Andre) #10

you missed a closing bracket } after line where

return value, ok

(Hemanth Naidu) #11

Can I perform the same operation using a struct?


(Andre) #12

map is a kind of struct too, but if you want to return a struct from resetCause you would define a struct and return it from there


(Hemanth Naidu) #13

I have a problem in setting the gopath. Even after setting up the workspace as subpath within the the GOPATH. I’m not able to access the libraries. It throws me this error.

c:\go\src\github.com\smartmakers\drivers\go\driver (from $GOROOT)
C:\Go\go-work\src\src\github.com\smartmakers\drivers\go\driver (from $GOPATH) 

Am I missing something here?


(Andre) #14

run go env and post it here


(Norbert Melzer) #15

src is not part of the GOPATH, it has to live below that.

So you need to set GOPATH=C:\Go\go-work rather than GOPATH=C:\Go\go-work\src.


(Hemanth Naidu) #16
C:\Go\go-work>go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\userName\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Go\go-work
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\userName\AppData\Local\Temp\go-build137919522=/tmp/go-build -gno-record-gcc-switches

(Andre) #17

I would suggest you to put your code into C:\Go\go-work\src.
For example, if your project is someproject, then it should be in path
C:\Go\go-work\src\someproject and then do you imports.


(Hemanth Naidu) #18

I tried that too. I still can’t work around it.


(Andre) #19

this should be considered wrong because there can not be double src — check your paths


(Hemanth Naidu) #20

I checked that too corrected it. When I’m compiling (Atom) everything is fine now no errors. When executing the test commands for the code on commandLine the following error is thrown.

The system cannot find the file specified.

This is because i’m trying to import one of the libraries which I cannot execute without and the naming system of this library is poor.

My current path looks like this

C:\Go\go-work\src\gobps

considered this also. I guess this is how it looked after installation.