Golang中的数据结构

huangapple go评论84阅读模式
英文:

Data Structures in Golang

问题

作为一个来自Ruby和JS世界的人,我有一个关于Golang的问题,请耐心等待,如果这是一个相当简单的Go问题,请原谅我:)

在API中使用Gorilla toolkit,我不确定自己的想法是否完全正确。我一直在仔细阅读《Go编程语言》,但我还不是Go的专家。在发送JSON响应时,我一直在使用类似下面的方式发送一个对象:

{ "healthy": true, "version": "0.0.1" }

但我不确定是否最佳实践或者符合Go的惯例,是否应该创建像appHealth这样的临时结构,或者我是否过于像在JS中那样思考,我会简单地创建一个对象字面量并将其返回给客户端。请教我,智慧的gophers。

谢谢!

package main

import (
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/mux"
)

type appHealth struct {
	Healthy bool
	Version string
}

func health(w http.ResponseWriter, r *http.Request) {
	health := appHealth{true, "0.0.1"}
	json.NewEncoder(w).Encode(health)
}

func main() {
	port := os.Getenv("PORT")
	router := mux.NewRouter().StrictSlash(false)
	router.HandleFunc("/health", health)
	log.Fatal(http.ListenAndServe(":"+port, router))
}
英文:

Asking a Golang question as someone coming from the Ruby & JS worlds, so bear with me if this is a rather simple Go question Golang中的数据结构

Working with the Gorilla toolkit on an API, and I'm not sure if my thinking is totally correct on something. I've been reading through the thoroughly-excellent The Go Programming Language, but am decidedly not expert at Go yet. When sending back a JSON response, I've been doing something like the below to send back an object like this:

{ "healthy": true, "version": "0.0.1" }

But I'm not sure if it's a best practice or idiomatic to go to be creating one-off structures like appHealth or if I'm thinking too-much like I would in JS, where I'd just throw up an object literal and return the JSON-ified version of that to the client. Teach me, wise gophers.

Thank you!

package main

import (
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/mux"
)

type appHealth struct {
	Healthy bool
	Version string
}

func health(w http.ResponseWriter, r *http.Request) {
	health := appHealth{true, "0.0.1"}
	json.NewEncoder(w).Encode(health)
}

func main() {
	port := os.Getenv("PORT")
	router := mux.NewRouter().StrictSlash(false)
	router.HandleFunc("/health", health)
	log.Fatal(http.ListenAndServe(":"+port, router))
}

答案1

得分: 1

看起来很不错。不要害怕为增加含义而创建类型。

type Version string

然后,你可以为版本定义逻辑,与该类型相关联。

或者你可能想要查看 juju 的版本包

如果你真的有一个只用一次的结构体,你可以使用匿名结构体和字面量来创建。

health := struct{ 
    Health bool
    Version string
}{
    true,
    "1.2.3",
}
英文:

Looks pretty good. Don't be afraid to make types for that will add to the meaning.

type Version string  

You can then have logic for what a version looks like that will be tied to the type

Or you might want to check out juju's version package.

If you really have a struct that is a one off, you can do an anonymous struct with a literal.

health := struct{ 
    Health bool
    Version string
}{
    true,
    "1.2.3",
}

huangapple
  • 本文由 发表于 2016年4月17日 07:18:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/36670906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定