编译/ json中是否存在未包含未导出字段的技术原因?如果没有,这是一个任意的决定可以有一个额外的后门选项(说'+')包括即使未导出?
要求客户端代码导出以获得此功能感觉很不幸,特别是如果小写提供封装或者对编组结构的决定要比它们的设计晚得多.
人们如何处理这个问题?只出口一切?
此外,不导出字段名称使得难以遵循建议的习语.我认为如果结构X有字段Y,则不能有一个存取方法Y().如果你想提供对Y的界面访问,你必须为getter提出一个新的名字,不管你会得到什么非惯用的东西,根据http://golang.org/doc/effective_go.html#Getters
开始学习golang.任务:获取Json和Unmarshall.但我错了:
Json tag but not exported
Run Code Online (Sandbox Code Playgroud)
如何导出未导出的字段,然后使用方法实现它?
这是代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Time struct {
time
}
type time struct {
id string `json:"$id"`
currentDateTime string `json:"currentDateTime,string"`
utcOffset float64 `json:"utcOffset,string"`
isDayLightSavingsTime bool `json:"isDayLightSavingsTime,string"`
dayOfTheWeek string `json:"dayOfTheWeek,string"`
timeZoneName string `json:"timeZoneName,string"`
currentFileTime float64 `json:"currentFileTime,string"`
ordinalDate string `json:"ordinalDate,string"`
serviceResponse string `json:"serviceResponse,string"`
}
func (t *Time) GetTime() (Time, error) {
result := Time{}
return result, t.Timenow(result)
}
func (t *Time) Timenow(result interface{}) error {
res, err := http.Get("http://worldclockapi.com/api/json/utc/now") …Run Code Online (Sandbox Code Playgroud) 我有一个json文件(themes/snow/theme.json)
{
Name:'snow',
Bgimage:'background.jpg',
Width:600,
Height:400,
Itemrotation:'20,40',
Text:{
Fontsize:12,
Color:'#ff00ff',
Fontfamily:'verdana',
Bottommargin:20
},
Decoration:[
{
Path:'abc.jpg',
X:2,
Y:4,
Rotation:0
},
{
Path:'def.png',
X:4,
Y:22,
Rotation:10
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有一个解析json数据的文件
package main
import (
"fmt"
"os"
"encoding/json"
"io/ioutil"
"log"
)
const themeDirectory = "themes"
const themeJsonFile = "theme.json"
type TextInfo struct {
Fontsize int
Color string
Fontfamily string
Bottommargin int
}
type DecoInfo struct {
Path string
X int
Y int
Rotation int
}
type ThemeInfo struct {
Name string
Bgimage …Run Code Online (Sandbox Code Playgroud) 我是一名新的 Go 程序员(来自 Java),我想重现一种在 Java 中易于使用的通用方式。
我想创建一些函数,允许我对 JSON 字符串执行解组,以避免代码重复。
这是我当前不起作用的代码:
type myStruct1 struct {
id string
name string
}
func (obj myStruct1) toString() string {
var result bytes.Buffer
result.WriteString("id : ")
result.WriteString(obj.id)
result.WriteString("\n")
result.WriteString("name : ")
result.WriteString(obj.name)
return result.String()
}
func main() {
content := `{id:"id1",name="myName"}`
object := myStruct1{}
parseJSON(content, object)
fmt.Println(object.toString())
}
func parseJSON(content string, object interface{}) {
var parsed interface{}
json.Unmarshal([]byte(content), &parsed)
}
Run Code Online (Sandbox Code Playgroud)
这段代码在运行时返回给我:
id :
name :
Run Code Online (Sandbox Code Playgroud)
你有什么主意吗 ?
谢谢
我有json未解码为struct.
我知道我的代码中的错误,但我被卡住了,不知道错误在哪里以及我做错了什么
请帮帮我,这是我的代码:
type GOOGLE_JSON struct {
code string `json:"code"`
clientId string `json:"clientId"`
redirectUri string `json:"redirectUri"`
}
body := []byte(`{"code":"111","clientId":"222","redirectUri":"333"}`)
//google_json := GOOGLE_JSON{}
var google_json GOOGLE_JSON
err := json.Unmarshal(body, &google_json)
if err != nil {
fmt.Println(err)
}
fmt.Println(google_json)
Run Code Online (Sandbox Code Playgroud)
我很困惑.当我用以下身体发帖时
{"lng":1.23, "lat":4.56,"utc":789}
Run Code Online (Sandbox Code Playgroud)
这个返回{0,0,0}(不正确)
func test(rw http.ResponseWriter, req *http.Request) {
type data struct {
lng float64
lat float64
utc int
}
decoder := json.NewDecoder(req.Body)
var t data
err := decoder.Decode(&t)
if err != nil {
panic("PANIC")
}
log.Println(t)
}
Run Code Online (Sandbox Code Playgroud)
这个返回{1.23,4.56,789}(正确)
func test(rw http.ResponseWriter, req *http.Request) {
type data struct {
Lng float64
Lat float64
Utc int
}
decoder := json.NewDecoder(req.Body)
var t data
err := decoder.Decode(&t)
if err != nil {
panic("PANIC")
}
log.Println(t)
}
Run Code Online (Sandbox Code Playgroud)
唯一的区别是我在结构定义中使用大写字母.我错过了什么吗?这是一个错误吗?
我有一个像这样的 JSON 文件:
[
{
"namespace": "pulsarNamespace1",
"name": "pulsarFunction1",
"tenant": "pulsarTenant1"
},
{
"namespace": "pulsarNamespace2",
"name": "pulsarFunction2",
"tenant": "pulsarTenant1"
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试将此 JSON 数组反序列化/解组为一个结构片,但我得到的结构为空(默认)值。
当我运行我的代码时,它正确地将我的文件读入一个字符串,但它没有正确反序列化数据,只是将空结构写入控制台,如下所示:
[]main.Config{main.Config{namespace:"", tenant:"", name:""}, main.Config{namespace:"", tenant:"", name:""}}
命名空间:名称:%!d(string=)
命名空间:名称:%!d(string=)
这是我在 Go 中的代码:
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
// Ignore the unused imports.
type Config struct {
namespace string `json:"namespace,omitempty"`
tenant string `json:"tenant,omitempty"`
name string `json:"name,omitempty"`
}
func getConfigs() string {
b, err := ioutil.ReadFile("fastDeploy_example.json") // just pass the file name …Run Code Online (Sandbox Code Playgroud) 我正在编写一个代码,将一个库存扫描到一个结构中,以便将它导出到json.
目前,我的代码使用该ScanDir函数精确地扫描了一个库,但是当我尝试对我的结构进行Marshal时它只会返回{}.
// file's struct
type Fic struct {
nom string `json:"fileName"`
lon int64 `json:"size"`
tim time.Time `json:"lastFileUpdate"`
md5hash []byte `json:"md5"`
}
// folder's struct
type Fol struct {
subFol []Fol `json:"listFolders"`
files []Fic `json:"listFiles"`
nom string `json:"folderName"`
tim time.Time `json:"lastFolderUpdate"`
}
func main() {
var root Fol
err := ScanDir("./folder", &root) // scan a folder and fill my struct
check(err)
b, err := json.Marshal(root)
check(err)
os.Stdout.Write(b)
}
func check(err error) {
if err != nil …Run Code Online (Sandbox Code Playgroud)