小编Kyl*_*yle的帖子

如何重定向到网址

我想在从上一页收集数据后向客户显示另一页.但我无法在服务器端重定向新的URL.这是我的逻辑:

  1. 使用POST操作向服务器提交用户输入;
  2. 服务器运行函数saveChoice()将用户输入保存到数据库中;
  3. 保存用户输入后,服务器向客户端发送新的URL;
  4. 当客户端获取新URL时,服务器将读取数据库并获取保存的数据

我坚持第3步(这是流程的一个例子):

type Stuff struct{
    List []string
}

func checkcheck(w http.ResponseWriter, r *http.Request) {
    sinfo := Stuff{
        List: some_slice
    }

    t, err := template.New("").Parse(tpl_ds)
    checkErr(err)
    err = r.ParseForm()
    checkErr(err)
    err = t.Execute(w, sinfo)
    checkErr(err)

    if r.Method == "POST" {
        saveChoice(r.Form["choices"])
        /* step 3: make user open another URL */
    }
}
Run Code Online (Sandbox Code Playgroud)

这是模板:

<html>
<script>
  $(function () {
    $('form').on('submit', function (e) {
      e.preventDefault();
      $.ajax({
        type: 'post',
        data: $('form').serialize(),
      });
    });
  });
</script>
<body>
  <form method="POST">
    {{range .List}} …
Run Code Online (Sandbox Code Playgroud)

go

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

Sqlite并发写作性能

我正在用Golang和Sqlite3编写一个网站,我希望每天大约有1000个并发写入,每天几分钟,所以我做了以下测试(忽略错误检查看起来更干净):

t1 := time.Now()
tx, _ := db.Begin()
stmt, _ := tx.Prepare("insert into foo(stuff) values(?)")
defer stmt.Close()

for i := 0; i < 1000; i++ {
    _, _ = stmt.Exec(strconv.Itoa(i) + " - ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,./;'[]-=<>?:()*&^%$#@!~`")
}

tx.Commit()
t2 := time.Now()
log.Println("Writing time: ", t2.Sub(t1))
Run Code Online (Sandbox Code Playgroud)

写作时间约为0.1秒.然后我将循环修改为:

for i := 0; i < 1000; i++ {
    go func(stmt *sql.Stmt, i int) {
        _, err = stmt.Exec(strconv.Itoa(i) + " - ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,./;'[]-=<>?:()*&^%$#@!~`")
        if err != nil {
            log.Fatal(err)
        }
    }(stmt, i)
}
Run Code Online (Sandbox Code Playgroud)

这让我神圣46.2秒!我跑了很多次,每次超过40秒!有时甚至超过一分钟!由于Golang同时处理每个用户,这是否意味着我必须切换数据库才能使网页正常工作?谢谢!

sqlite go

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

手机兼容性

我正在使用 gomobile 和本机 golang 编写一些测试应用程序。奇怪的是,这些应用程序无法在运行 Android 4.1.1 的 Galaxy S3 上运行,但可以在运行 Android 5.1.1 的 Galaxy Note 5 上运行。有谁知道当前版本的gomobile的明确硬件和软件要求?我认为任何文件或演讲中都没有这样的内容。

go gomobile

5
推荐指数
1
解决办法
395
查看次数

如何以相同的顺序运行多个goroutine并收集结果

我有以下代码,它具有double-go例程结构:

package main

import(
    "fmt"
    "math/rand"
    "time"
    "strconv"
)

func main(){
    outchan := make(chan string)
    for i:=0;i<10;i++{
        go testfun(i, outchan)
    }
    for i:=0;i<10;i++{
        a := <-outchan
        fmt.Println(a)
    }
}

func testfun(i int, outchan chan<- string){
    outchan2 := make(chan int)
    time.Sleep(time.Millisecond*time.Duration(int64(rand.Intn(10))))
    for j:=0;j<10;j++ {
        go testfun2(j, outchan2)
    }
    tempStr := strconv.FormatInt(int64(i),10)+" - "
    for j:=0;j<10;j++ {
        tempStr = tempStr + strconv.FormatInt(int64(<-outchan2),10)
    }
    outchan <- tempStr
}

func testfun2(j int, outchan2 chan<- int){
    time.Sleep(time.Millisecond*time.Duration(int64(rand.Intn(10))))
    outchan2 <- j
}
Run Code Online (Sandbox Code Playgroud)

我期待的输出是

0 - 0123456789 …
Run Code Online (Sandbox Code Playgroud)

go

4
推荐指数
1
解决办法
1952
查看次数

标签 统计

go ×4

gomobile ×1

sqlite ×1