dongpochi9741 2013-08-17 16:14 采纳率: 100%
浏览 815

此websocket网址“ ws:// {{$}} / ws”是什么意思?

I work with websocket in go. And I got a websocket url format from a trivial example that I google like this:


Relatively complete code below:


<title>Chat Example</title>
<script type="text/javascript">
    $(function() {
        if (window["WebSocket"]) {
            conn = new WebSocket("ws://{{$}}/ws");
            conn.onclose = function(evt) {
                appendLog($("<div><b>Connection closed.</b></div>"))
            conn.onmessage = function(evt) {
        } else {
            appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))

And wsServer.go:

package main

import (
var addr = flag.String("addr", ":8080", "http service address")
var homeTempl = template.Must(template.ParseFiles("home.html"))

func serveHome(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    homeTempl.Execute(w, r.Host)

func main() {
    http.HandleFunc("/", serveHome)
    http.HandleFunc("/ws", serveWs)
    err := http.ListenAndServe(:8080, nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)

I thought it would be a regular expression while actually I can't explain it.

I test it on my own PC browser, and connect success with:



http://ip:8080 (which ip is my computer's also the litsening server's ip)

not. And why?

Of course it works when I change "ws://{{$}}/ws" to a certain url. But I want to know why?And what can this expression matching for?

The complete example code is large, I think above is enough to the question. If I miss something you can find out complete example in this page :

  • 写回答

2条回答 默认 最新

  • donglijuan8227 2013-08-17 18:21

    I'm guessing you are using the template package of Go. The template package supports {{ placeholders }} that are annotated by those curly brackets. Those curly brackets might contain statements like range, if etc, and variable names. The variable name $ is a special name that points to the root element that was passed to the template.Execute method.

    Please add the code of your wsServe method so that we can see what value your are passing to your template. I will extend my answer afterwards.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模