小编Dav*_*e C的帖子

如何在golang中的字符串上使用gzip?

我想用去从读出一大块string,把它作为一个gzip和string这个块.我知道如何从中读取compress/gzip并将其视为一种io.writer,但是当涉及到buf我迷失了.我应该创建一个gzip.NewWriter(io.writer),它写入w *gzip.Writer(字节片),用w.Write(chunk_of_file)获得的作家指针buf..和然后使用string写gzip来string?然后我需要将字符串视为字节切片..

gzip go

9
推荐指数
1
解决办法
1万
查看次数

Golang模板范围中的最后一项

鉴于模板

{{range $i, $e := .SomeField}}
        {{if $i}}, {{end}}
        $e.TheString
{{end}}
Run Code Online (Sandbox Code Playgroud)

这可以输出

one, two, three
Run Code Online (Sandbox Code Playgroud)

但是,如果我想输出

one, two, and three
Run Code Online (Sandbox Code Playgroud)

我需要知道哪个是上面范围内的最后一个元素.

我可以设置一个变量来保存数组的长度.SomeField,但是它总是3,而上面的$ i值只会变为2.你不能在我看到的模板中执行算术运算.

是否可以检测模板范围中的最后一个值?干杯.

go go-templates

9
推荐指数
1
解决办法
1万
查看次数

如何找到"无效字符","寻找值的开头"错误信息

我有一个简短的Go程序,它运行go list -json几个包的命令,将每个命令运行的输出存储在json.RawMessage中,将每个json.RawMessage追加到一个json.RawMessages片中,然后将结果返回给服务器将每个json.RawMessages连接在一起并压缩json之后.但是,当我运行时json.Compact无法找到源的错误消息.谷歌搜索此错误消息表明,大多数似乎遇到它的人 - 无论是无效,还是其他角色 - 都很难找到它的来源.

invalid character ',' looking for beginning of value
Run Code Online (Sandbox Code Playgroud)

带有注释的代码可以在play.golang.org上找到(尽管它不会在那里运行),也可以在下面查看.

问题:您能解释一下这个错误的来源以及如何预防吗?

(注意,一些软件包仅用于测试目的)

package main

import (
    "expvar"

    "encoding/json"

    "bytes"
    "fmt"
    "github.com/go-martini/martini"
    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
    "go/build"
    "log"
    "math/rand"
    "net/http"
    _ "net/http/pprof"
    "os/exec"
)

type myType struct {
    J []json.RawMessage
}

var pack map[string]string

type GoList struct {
    Imports []string
}

type Import struct {
    Dir        string
    ImportPath string
    Name       string
    Target     string
    Standard   bool
    Root       string
    GoFiles    []string …
Run Code Online (Sandbox Code Playgroud)

go

8
推荐指数
4
解决办法
6万
查看次数

如何为 Gorm 指定具有多列唯一索引的结构?

如何定义我的structs 以在 Go 中为 Gorm 指定多列唯一索引?

如:

type Something struct {
    gorm.Model
    First  string `sql:"unique_index:unique_index_with_second"`
    Second string `sql:"unique_index:unique_index_with_first"`
}
Run Code Online (Sandbox Code Playgroud)

go go-gorm

8
推荐指数
3
解决办法
5264
查看次数

如何使用Go的Google Spreadsheet API?

我需要能够读取和写入电子表格的数据.我正在使用Google App Engine中的Go语言进行编程.

google-app-engine google-api go

7
推荐指数
1
解决办法
2456
查看次数

Golang:如何使用基本身份验证提供静态文件

我无法使用go-http-auth对http.FileServer进行基本身份验证.

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/abbot/go-http-auth"
)

func Secret(user, realm string) string {
    users := map[string]string{
        "john": "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1", //hello
    }

    if a, ok := users[user]; ok {
        return a
    }
    return ""
}

func doRoot(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>static file server</h1><p><a href='./static'>folder</p>")
}

func handleFileServer(w http.ResponseWriter, r *http.Request) {
    fs := http.FileServer(http.Dir("static"))
    http.StripPrefix("/static/", fs)
}

func main() {

    authenticator := auth.NewBasicAuthenticator("localhost", Secret)

    // how to secure the FileServer with basic authentication??
    // fs := http.FileServer(http.Dir("static")) …
Run Code Online (Sandbox Code Playgroud)

fileserver go basic-authentication

7
推荐指数
1
解决办法
4248
查看次数

在golang中解析Json对象的Json数组?

我试图从json数组中获取每个json对象.我通过http帖子获得这些数据.

我知道我的数据会是什么样子:

   {
    "array":[
       {
          "entity_title":"University of Phoenix", 
          "entity_org_name":"CS Club",
          "possible_user_name":"Johnny Ive",
          "posibble_user_email":"Johhny.Ive@uop.edu",
          "user_position_title":"President",
          "msg_body_id":4
       },
      {
          "entity_title":"University of San Francisco", 
          "entity_org_name":"Marketing club",
          "possible_user_name":"steve jobs",
          "posibble_user_email":"steven.job@uop.edu",
          "user_position_title":"Student",
          "msg_body_id":5
      }
    ]
  }
Run Code Online (Sandbox Code Playgroud)

我的示例代码和我的结构看起来像这样:

    type MsgCreateUserArray struct {
         CreateUser []MsgCreateUserJson `json:"createUserArray"`
    }
    type MsgCreateUserJson struct {
        EntityTitleName string  `json:"entity_title_name"`
        EntityOrgName   string  `json:"entity_org_name"`
        PossibleUserName string `json:"possible_user_name"`
        PossibleUserEmail   string  `json:"possible_user_email"`
        UserPositionTitle   string  `json:"user_position_title"`
        MsgBodyId       string  `json:"msg_body_id, omitempty"` 
    }


func parseJson(rw http.ResponseWriter, request *http.Request) {
    decodeJson := json.NewDecoder(request.Body)

    var msg MsgCreateUserArray
    err := decodeJson.Decode(&msg)

    if …
Run Code Online (Sandbox Code Playgroud)

arrays json go

7
推荐指数
1
解决办法
1万
查看次数

适用于Windows和Linux的Go命名管道

我是Go的新手,我想在Golang中创建Named Pipes实现,它适用于Windows和Linux.

我设法让代码在Ubuntu上运行,但是这个代码在Windows上不起作用

Go中没有任何抽象,允许您在两个环境中使用命名管道

下面是我的代码

//to create pipe: does not work in windows    
syscall.Mkfifo("tmpPipe", 0666)    

// to open pipe to write    
file, err1 := os.OpenFile("tmpPipe", os.O_RDWR, os.ModeNamedPipe)    

//to open pipe to read    
file, err := os.OpenFile("tmpPipe", os.O_RDONLY, os.ModeNamedPipe)
Run Code Online (Sandbox Code Playgroud)

任何帮助或指针都会有很大帮助.谢谢

named-pipes go

7
推荐指数
2
解决办法
6916
查看次数

什么是正确的golang方式来做到这一点:

这看起来有点愚蠢,肯定有更好的方法吗?

err = SendMessageAndWait(db, "this is a test")
if err != nil {
    fmt.Println("Error sending message", err)
    return
}
err = DoSomething(db, "this is a test")
if err != nil {
    fmt.Println("Error sending message", err)
    return
}
err = CheckSomething(db, "this is another test")
if err != nil {
    fmt.Println("Error sending message", err)
    return
}
err = SendMessageAndWait(db, "this is a third test")
if err != nil {
    fmt.Println("Error sending message", err)
    return
}
... x10 ...
Run Code Online (Sandbox Code Playgroud)

go

6
推荐指数
1
解决办法
526
查看次数

goroutines如何运作?

我正在关注Go Tour,当涉及到goroutines时我有点卡住了.我知道他们非常轻量级,每次goroutine阻止,另一个将开始,但我无法理解这个例子实际如何工作:

package main

import (
    "fmt"
    "time"
)

func say(s string) {
    for i := 0; i < 5; i++ {
        time.Sleep(1000 * time.Millisecond)
        fmt.Println(s)
    }
}

func main() {
    go say("world")
    say("hello")
}
Run Code Online (Sandbox Code Playgroud)

Playground

据我所知,一个goroutine是针对具有参数"world"的say函数启动的,但据我所知,应该打印"world"五次并且"hello"一次.但是我不明白为什么输出是这样的:

hello
world
hello
world
hello
world
hello
world
hello
Run Code Online (Sandbox Code Playgroud)

从我对其他语言的线程的有限理解,输出应该是这样的:

hello
world
world
world
world
world
Run Code Online (Sandbox Code Playgroud)

或者像这样:

world 
world
world
hello
world
world
Run Code Online (Sandbox Code Playgroud)

为什么第二行也执行五次?go语句下面的任何内容都会归类为go例程的一部分吗?

下一张幻灯片还显示了我无法再次回头看的内容:

package main

import "fmt"

func sum(a []int, c chan int) {
    sum := 0 …
Run Code Online (Sandbox Code Playgroud)

concurrency go goroutine

6
推荐指数
1
解决办法
1132
查看次数