动态创建结构

This script does not work. How may I create a new struc dynamically by getting the types of incoming variables like proc.ReadStat ? (type of proc.ReadStat is *linux.Stat as shown by reflect.TypeOf() )

package main

import (
    "fmt"
    "reflect"
    //"html"
    "log"
    "net/http"
    //"time"
    "encoding/json"
    "github.com/gorilla/mux"
    proc "github.com/c9s/goprocinfo/linux"
)



func sysinfo(w http.ResponseWriter, r *http.Request) {


    sstat, _ := proc.ReadStat("/proc/stat")
    sdiskstats, _ := proc.ReadDiskStats("/proc/diskstats")
    sloadavg, _ := proc.ReadLoadAvg("/proc/loadavg")
    smeminfo,_ := proc.ReadMemInfo("/proc/meminfo")
    smounts, _ := proc.ReadMounts("/proc/mounts")
    snetstat, _ := proc.ReadNetStat("/proc/net/netstat")
    sdevstat, _ := proc.ReadNetworkStat("/proc/net/dev")
    ssockstat, _ := proc.ReadSockStat("/proc/net/sockstat")
    svmstat, _ := proc.ReadVMStat("/proc/vmstat")

t   /* type Info interface {
        stat      *linux.Stat
        diskstats []linux.DiskStat
        loadavg   *linux.LoadAvg
        meminfo   *linux.MemInfo
        mounts    *linux.Mounts
        netstat   *linux.NetStat
        devstat   []linux.NetworkStat
        sockstat  *linux.SockStat 
        vmstat    *linux.VMStat
    }*/
    type Info struct {
        stat       reflect.TypeOf(sstat)
        diskstats  reflect.TypeOf(sdiskstats)
        loadavg    reflect.TypeOf(sloadavg)
        meminfo    reflect.TypeOf(smeminfo)
        mounts     reflect.TypeOf(smounts)
        netstat    reflect.TypeOf(snetstat)
        devstat    reflect.TypeOf(sdevstat)
        sockstat   reflect.TypeOf(ssockstat)
        vmstat     reflect.TypeOf(svmstat)
    }

    type infos []Info


    infos := info{
        stat : sstat,
        diskstats : sdiskstats,
        loadavg : sloadavg,
        meminfo : smeminfo,
        mounts : smounts,
        netstat : snetstat,
        devstat : sdevstat,
        sockstat : ssockstat,
        vmstat : svmstat,
    }

    json.NewEncoder(w).Encode(infos)


}

func main() {

    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/", sysinfo)
    log.Fatal(http.ListenAndServe(":8080", router))

}

In statically typed languages (Go is one of them) type info (of variables, fields etc.) have to be present and compile-time, so you can't do something like:

type Info struct {
    stat       reflect.TypeOf(sstat)
}

Because reflect.TypeOf() is a function which will run at run-time. If you don't know the type of something, you may use interface{}, but then you won't get any compile-time type checking from the compiler.

Since you just want to marshal the results, you can use interface{} as the type of the fields. Also note that field names must be exported (must start with a capital letter) in order to be included in JSON output.

So try the following:

type Info struct {
    Stat       interface{}
    Diskstats  interface{}
    Loadavg    interface{}
    Meminfo    interface{}
    Mounts     interface{}
    Netstat    interface{}
    Devstat    interface{}
    Sockstat   interface{}
    Vmstat     interface{}
}

type infos []Info

infos := info{
    Stat : sstat,
    Diskstats : sdiskstats,
    Loadavg : sloadavg,
    Meminfo : smeminfo,
    Mounts : smounts,
    Netstat : snetstat,
    Devstat : sdevstat,
    Sockstat : ssockstat,
    Vmstat : svmstat,
}

json.NewEncoder(w).Encode(infos)

Note that you could also just use a simple map[string]interface{}:

infos := map[string]interface{}{
    "stat" : sstat,
    "diskstats" : sdiskstats,
    "loadavg" : sloadavg,
    "meminfo" : smeminfo,
    "mounts" : smounts,
    "netstat" : snetstat,
    "devstat" : sdevstat,
    "sockstat" : ssockstat,
    "vmstat" : svmstat,
}
json.NewEncoder(w).Encode(infos)

Also please do not forget about and do not omit error handling: store returned errors and check whether they are nil!