Access value using key from a struct

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).

2 Likes

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
}
3 Likes

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 (
2 Likes

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)
	}
}
2 Likes

Still same error.

2 Likes

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

2 Likes

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

2 Likes

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

2 Likes

Thanks @cinematik. What was causing this error?

2 Likes

you missed a closing bracket } after line where

return value, ok
2 Likes

Can I perform the same operation using a struct?

2 Likes

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

2 Likes

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?

2 Likes

run go env and post it here

2 Likes

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.

2 Likes
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
2 Likes

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.

2 Likes

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

2 Likes

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

2 Likes

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.

2 Likes