hi,

I am a newbie at GO Programming.  Here is scenario :-

There exists a JSON file that looks like this :-

{
        "template": "linuxbase1",
        "checkname": ["check_disk"],
        "checkmethod": ["check_disk"]
}

I am Unmarshalling this data into a structure :-
---------------------------------------
package func1

import (
        "io/ioutil"
        "os"
        "encoding/json"
        "fmt"
)

type pluginfunc func() string
type Plugindata struct {
        Template string `json:"template"`
        Checkname []string `json:"checkname"`
        Checkmethod []pluginfunc `json:"checkmethod"`
}

var (
        Templatepath = "json_sample1.json"
        Templateitems Plugindata
)

func Gettemplatedata() {
        tdata, err := ioutil.ReadFile(Templatepath)
        if err != nil {
                fmt.Printf("Unable to read file %s. Error - 
%v\n",Templatepath, err.Error())
                os.Exit(3)
        }
        json.Unmarshal(tdata, &Templateitems)
}
---------------------------------------

The "check_disk" function is here :-
----------
package func1

func check_disk() string {
        return "Called check_disk"
}
---------


This is the program with main() :-
---------------------------------
package main

import (
        "fmt"
        "checksexpt/func1"
)

func main() {
        func1.Gettemplatedata()
        fmt.Printf("Templateitems in Main() => %v\n",func1.Templateitems)
        for index,funcname := range func1.Templateitems.Checkmethod {
                fmt.Printf("%d = %s\n",index,funcname())
        }

}
---------------------------------


As expected, when I run main(); I see the error :-
---------------------------
Templateitems in Main() => {linuxbase1 [check_cpu check_disk] [<nil> <nil>]}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x40115e]

goroutine 1 [running]:
panic(0x50e980, 0xc82000a100)
        /opt/go/src/runtime/panic.go:481 +0x3e6
---------------------------

So, I am trying to grab a string from the JSON file and treat it as a 
function call. That obviously fails !  But, the primary constraint here is 
that the function names have to be picked from the JSON file. How can I do 
this ?  I know that I can create a static map as follows :-

-----------------
type checkfunc func() string
var (
        Templateitems = map[string]map[string]checkfunc {
                "linuxbase1": {
                        "check_disk": check_disk,
                },
        }
)
---------------
So, A call like - Templateitems["linuxbase1"]["check_disk"]() would work 
just fine. But, I dont want to create any such static map as the elements 
in that map needs to keep growing. Any ideas on this, please ?


Thanx !

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to