Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitals in struct fields

Tags:

json

struct

go

I'm using this library to access CouchDB (cloudant to be specific) "github.com/mikebell-org/go-couchdb" and I've noticed a problem.

When I go to add a file to the database and pass in a struct, only the fields of the struct which started with a capital letter get added.

For example

type Person struct {
    name string
    Age  int
}

func main() {
    db, _ := couchdb.Database(host, database, username, password)
    joe := Person{
        name: "mike",
        Age:  190,
    }
    m, _ := db.PostDocument(joe)
}

In this case, only the "age" field got updated and inserted into my database.

I've noticed this problem in another case also - when I'm doing something like this :

type Sample struct {
    Name string
    age  int 
}


joe := Sample{
    Name: "xx",
    age:  23,
}

byt, _ := json.Marshal(joe)

post_data := strings.NewReader(string(byt))
fmt.Println(post_data)

in this case, only Name would be printed out :

output : &{{"Name":"xx"} 0 -1}

Why is this? and If I would like to have a field with a lowercase and be inside the database, is that possible?

like image 576
Saif Abid Avatar asked Jul 19 '14 06:07

Saif Abid


People also ask

Should struct name be capitalized Golang?

Capitalized Identifiers are exported. The capital letter indicates that this is an exported identifier and is available outside the package.

When should you capitalize a word?

In general, you should capitalize the first word, all nouns, all verbs (even short ones, like is), all adjectives, and all proper nouns. That means you should lowercase articles, conjunctions, and prepositions—however, some style guides say to capitalize conjunctions and prepositions that are longer than five letters.

What is it called when the first letter of each word is capitalized?

Capitalization (American English) or capitalisation (British English) is writing a word with its first letter as a capital letter (uppercase letter) and the remaining letters in lower case, in writing systems with a case distinction.

What does it mean to capitalize every other letter?

Alternating caps are typically used to display mockery in text messages. The randomized capitalization leads to the flow of words being broken, making it harder for the text to be read as it disrupts word identification even when the size of the letters is the same as in uppercase or lowercase.


2 Answers

This is because only fields starting with a capital letter are exported, or in other words visible outside the curent package (and in the json package in this case).

Here is the part of the specifications refering to this: http://golang.org/ref/spec#Exported_identifiers

Still, you can unmarshall json fields that do no start with a capital letters using what is called "tags". With the json package, this is the syntax to use:

type Sample struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

Refer to the documentation for more information about this.

like image 70
julienc Avatar answered Oct 12 '22 12:10

julienc


json package only stringfiy fields start with capital letter. see http://golang.org/pkg/encoding/json/

Struct values encode as JSON objects. Each exported struct field becomes a member of the object, using the field name as the object key, unless the field is omitted for one of the reasons given below.

You need define the struct like this:

type Sample struct{
    Name string `json:"name"`
    Age int `json:"age"`
}
like image 32
chendesheng Avatar answered Oct 12 '22 12:10

chendesheng