我是golang的新手,他从动态类型语言转变而来.
我面临着如何编写具有许多类别/子类别的目录的问题 - 复杂的分类法.例如:
鞋带>鞋子>男士>鞋子>衣服>首页>分类
我使用mongodb作为后端.在这种情况下,我无法理解如何编写CRUD操作?
如果我像往常一样处理所有查询:
func RunFindAllQuery(document interface{}, m bson.M, mongoSession *mgo.Session, conn Conn) (err error) {
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
collection:= sessionCopy.DB(conn.Database).C(conn.Collection)
err = collection.Find(m).All(document)
if err != nil {
log.Printf("RunQuery : ERROR : %s\n", err)
}
return err
}
Run Code Online (Sandbox Code Playgroud)
我需要定义很多类型:鞋子!=汽车.
type Shoes struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Description string `bson:"descriprion"`
Size float `bson:"size"`
Color float `bson:"color"`
Type float `bson:"type"`
ShoeHeight float `bson:"shoeheight"`
PlatformHeight float `bson:"platformheight"`
Country float `bson:"country"` …Run Code Online (Sandbox Code Playgroud) 我有 IntelliJ IDEA Ultimate 2018.2.3。我正在尝试从 IDE 中的差异(比较...)创建补丁,但我找不到这样做的按钮。我已经在 branch_A 上提交了几次。我不需要从本地更改创建补丁,我需要从与另一个分支的差异创建补丁。
在 git 中很简单 git diff branch_A branch_B >> diff.patch
有没有办法在IDE中实现它?
我正在寻找一种干净的方法将字节数组转换为客户端-服务器应用程序的结构。我知道大多数人都转向 gob 包来实现此解决方案,但是我不控制应用程序的编码。话虽这么说,我只编写了服务器应用程序而不是客户端,正在交换的协议有一个相互契约。
我能得出的最好的结果如下。
type T struct {
A int16
B int8
C []byte
}
func main() {
// Create a struct and write it.
t := T{A: 99, B: 10}
buf := &bytes.Buffer{}
buf1 := []byte{5, 100, 100}
fmt.Println(buf1)
buf.Write(buf1)
//err := binary.Write(buf, binary.BigEndian, t)
//if err != nil {
// panic(err)
//}
fmt.Println(buf)
// Read into an empty struct.
t = T{}
err := binary.Read(buf, binary.BigEndian, &t)
if err != nil {
panic(err)
}
fmt.Printf("%d %d", t.A, t.B) …Run Code Online (Sandbox Code Playgroud) 在下面的示例中:
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
p := new(Proxy)
//host := "www.google.com" // WORKS AS EXPECTED
host := "www.apple.com" // GIVES AN ERROR
u, err := url.Parse(fmt.Sprintf("http://%v/", host))
if err != nil {
log.Printf("Error parsing URL")
}
p.proxy = httputil.NewSingleHostReverseProxy(u)
http.Handle("/", p)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
type Proxy struct {
proxy *httputil.ReverseProxy
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.proxy.ServeHTTP(w, r)
}
Run Code Online (Sandbox Code Playgroud)
将Chrome链接到'localhost:8000'时,将'www.google.com'与'www.apple.com'交换会导致此错误:
无效的网址
请求的URL"/"无效.参考编号#9.a61a32b8.1438231668.41733295
为www.apple.com做一点挖掘,我得到:
? ~ curl --ipv4 -v …Run Code Online (Sandbox Code Playgroud) 我正在为我的公司群聊写一个地址簿机器人.机器人的主要思想是搜索用户电报联系人的能力.例如/find <first name & last name>搜索机器人的数据库和回复提到的已创建用户,因此人们可以与该人联系,点击其提及(通过[inline mention of a user](tg://user?id=123456789)).
问题是有时机器人得到ENTITY_MENTION_USER_INVALID.此外,几分钟后,提及该用户可以成功.作为一种解决方法,我尝试使用@userName而不是userId提及,但现在我又遇到了麻烦:并非所有人都拥有@userName.
所以问题是:如何避免ENTITY_MENTION_USER_INVALID错误?
在 Go 中,是否可以定义一个自定义类型,其中包含byte uint uint16除由其他内置类型提供的位数之外的位数?
我计划使用“刚好足够的位”来表示变量,并想要 6 位和 4 位类型。也许是复合布尔类型?
type fourbit struct{
ones bool
twos bool
fours bool
eights bool
}
Run Code Online (Sandbox Code Playgroud)
虽然这种事情相当混乱,但如果能有一个针对 n 位类型的更通用的解决方案就好了。