我遇到以下导入代码错误:
代码:包主
import (
"log"
"net/http"
"os"
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful/swagger"
"./api"
)
Run Code Online (Sandbox Code Playgroud)
错误:
.\main.go:9: imported and not used: "_/c_/Users/aaaa/IdeaProjects/app/src/api"
Run Code Online (Sandbox Code Playgroud)
鉴于我已经将package api文件存储在api文件夹下,因此导入为什么不起作用?
我正在下面使用api在main.go中
func main() {
// to see what happens in the package, uncomment the following
restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))
wsContainer := restful.NewContainer()
api := ApiResource{map[string]OxiResp{}}
api.registerLogin(wsContainer)
api.registerAccount(wsContainer)
api.registerLostLogin(wsContainer)
api.registerWallet(wsContainer)
}
Run Code Online (Sandbox Code Playgroud) 我想知道在退出程序之前等待程序完成的正确方法是什么.阅读其他一些答案似乎bool chan会做到这一点,就像Playground链接一样
func do_stuff(done chan bool) {
fmt.Println("Doing stuff")
done <- true
}
func main() {
fmt.Println("Main")
done := make(chan bool)
go do_stuff(done)
<-done
//<-done
}
Run Code Online (Sandbox Code Playgroud)
我这里有两个问题:
为什么< - 完成工作?
如果我取消注释最后一行会发生什么?我有一个死锁错误.这是因为频道是空的,没有其他功能向它发送值?
我需要在模板中使用地图,但是语法不正确
<tr>
<td>India</td>
<td>{{$val.Agent.Imei}}</td>
<td>{{$val.Jid}}</td>
<td>{{.LoginTimeMap[$val.Jid]}}
</tr>
Run Code Online (Sandbox Code Playgroud)
它说{{.LoginTimeMap [$ val.Jid]}}中的[]是命令中意外的坏字符U + 005B'['
请帮忙
如果我有一个这样的类型:
type myType ...
func (m myType) String() string { ... }
Run Code Online (Sandbox Code Playgroud)
如何fmt使用默认表示(即,而不是String()调用)打印(使用各种函数)此类型?我想做的是这样的事情:
func (m myType) String() string {
// some arbitrary property
if myType.isValid() {
// format properly
} else {
// will recurse infinitely; would like default
// representation instead
return fmt.Sprintf("invalid myType: %v", m)
}
}
Run Code Online (Sandbox Code Playgroud) 我是Go的新手,我在调试此客户端/服务器文件传输代码时遇到了麻烦.当我从服务器请求一个719KB png文件,我得到了719KB ..但不完全时,PNG,当我打开它并没有完全显示出来(一些被切断.我要去哪里错了吗?
// CLIENT ///
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"net"
"os"
"strings"
)
const BUFFER_SIZE = 1024
func main() {
//get port and ip address to dial
if len(os.Args) != 3 {
fmt.Println("useage example: tcpClient 127.0.0.1 7005")
return
}
var ip string = os.Args[1]
var port string = os.Args[2]
connection, err := net.Dial("tcp", ip+":"+port)
if err != nil {
fmt.Println("There was an error making a connection")
}
//read from
reader := bufio.NewReader(os.Stdin)
fmt.Print("Please enter …Run Code Online (Sandbox Code Playgroud) 我有以下代码,一切正常.
var view404 = template.Must(template.ParseFiles("views/404.html"))
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
err := view404.Execute(w, nil)
check(err)
}
func main() {
router := mux.NewRouter()
router.StrictSlash(true)
router.NotFoundHandler = http.HandlerFunc(NotFound)
router.Handle("/", IndexHandler).Methods("GET")
router.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
http.Handle("/", router)
http.ListenAndServe(":8000", nil)
}
Run Code Online (Sandbox Code Playgroud)
对路线的请求/cannot/find显示我的自定义404模板.我的/public/目录中的所有静态文件也正确提供.
我在处理不存在的静态文件并显示我的自定义NotFound处理程序时遇到问题.请求/public/cannot/find调用回复的标准http.NotFoundHandler
404页面不存在
如何为普通路由和静态文件使用相同的自定义NotFoundHandler?
更新
我最终FileHandler通过包装来实现我自己的http.ServeFile@Dewy Broto建议.
type FileHandler struct {
Path string
}
func (f FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
prefix := "public"
http.ServeFile(w, r, path.Join(prefix, f.Path))
}
// …Run Code Online (Sandbox Code Playgroud) 我正在创建一个RESTful Web服务(在Golang中),它从数据库中提取一组行并将其返回给客户端(智能手机应用程序或Web应用程序).该服务需要能够提供分页.唯一的问题是这个数据是在一个经常变化的"计算"列上排序的(例如,网站上的一段内容的"竖起大拇指"或"竖起大拇指"的数量),所以行可以跳转到页码在客户的请求之间.
我已经看了一些PostgreSQL功能,我可以用来帮助我解决这个问题,但似乎没有什么比这更好的解决方案了.
有没有人在客户端或数据库端有任何关于如何处理这个问题的建议?有什么我真的可以做,或者这是一个问题,通常只是由消费数据的客户补救?
编辑:我应该提到智能手机应用程序允许用户通过"无限滚动"查看更多的数据,因此它会跟踪它自己的数据客户端列表.
如果我在Go中使用C变量,我很困惑哪些变量需要被释放.
例如,如果我这样做:
s := C.CString(`something`)
Run Code Online (Sandbox Code Playgroud)
这个内存现在是在我调用之前分配的C.free(unsafe.Pointer(s)),还是可以在函数结束时由Go进行垃圾回收?
或者只是从导入的C代码创建的变量需要被释放,并且从Go代码创建的这些C变量将被垃圾收集?
我正在调用一个较老的api,并以其形式返回它的对象.
{ value: 1, time: "/Date(1412321990000)/" }
Run Code Online (Sandbox Code Playgroud)
使用定义的结构
type Values struct{
Value int
Time time.Time
}
Run Code Online (Sandbox Code Playgroud)
给我一个&time.ParseError.我是Go的初学者,有没有办法让我定义如何序列化/反序列化?最终我确实希望它作为time.Time对象.
这种日期格式似乎也是一种较旧的.NET格式.无法真正改变输出.
我有以下http客户端/服务器代码:
服务器
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Req: ", r.URL)
w.Write([]byte("OK")) // <== PROBLEMATIC LINE
// w.WriteHeader(200) // Works as expected
})
log.Fatal(http.ListenAndServe(":5008", nil))
}
Run Code Online (Sandbox Code Playgroud)
客户
func main() {
client := &http.Client{}
for i := 0; i < 500; i++ {
url := fmt.Sprintf("http://localhost:5008/%02d", i)
req, _ := http.NewRequest("GET", url, nil)
_, err := client.Do(req)
if err != nil {
fmt.Println("error: ", err)
} else {
fmt.Println("success: ", i)
}
time.Sleep(10 * time.Millisecond)
}
}
Run Code Online (Sandbox Code Playgroud)
当我在服务器上面运行客户端时,在250个连接后,我从client.Do获得以下错误: …
go ×10
c ×1
cgo ×1
channel ×1
formatting ×1
goroutine ×1
interface ×1
json ×1
map ×1
networking ×1
pagination ×1
postgresql ×1
printf ×1
rest ×1
sql ×1
tcp ×1