goophy

第 2287 号会员 /

回复了 gomgo 创建的主题: Revel SetCookie(cookie) 的问题

因为 v, _ := c.Request.Cookie(“Username”), Request 还是当前的Request,c.SetCookies在response后才生效。
用session比较方便吧

回复了 goophy 创建的主题: mgo, omitempty 字段的清空

好吧。 如果A在mongodb里已经有value,当struct里,A设置为 omitempty,如果web form 提交时A字段没填,用 mgo.Update(bson.M{"b": "bvalue", "a": "") 就不会更新A 的值为“”。 问题是,我确实需要让A成为“”。

回复了 teaey 创建的主题: 请大家帮忙看下,变量申明的问题

https://golang.org/doc/effective_go.html#redeclaration

Redeclaration and reassignment

An aside: The last example in the previous section demonstrates a detail of how the := short declaration form works. The declaration that calls os.Open reads,

f, err := os.Open(name) This statement declares two variables, f and err. A few lines later, the call to f.Stat reads,

d, err := f.Stat() which looks as if it declares d and err. Notice, though, that err appears in both statements. This duplication is legal: err is declared by the first statement, but only re-assigned in the second. This means that the call to f.Stat uses the existing err variable declared above, and just gives it a new value.

In a := declaration a variable v may appear even if it has already been declared, provided:

this declaration is in the same scope as the existing declaration of v (if v is already declared in an outer scope, the declaration will create a new variable §), the corresponding value in the initialization is assignable to v, and there is at least one other variable in the declaration that is being declared anew. This unusual property is pure pragmatism, making it easy to use a single err value, for example, in a long if-else chain. You'll see it used often.

§ It's worth noting here that in Go the scope of function parameters and return values is the same as the function body, even though they appear lexically outside the braces that enclose the body.

回复了 testaccount 创建的主题: revel的模板问题

try this
{{$posts := .posts}} {{range $post :=$posts}} {{$post.UserName}} ...

...{{with $field := field "comment.ContenStr" .}}