小编coo*_*aac的帖子

什么是C++在golang中的"using"等价物

什么是C++ using some_namespace::object在golang中的等价物?

根据这里的问题, 我可以using namespace common在下面说明:

import (
  . "common"
)
Run Code Online (Sandbox Code Playgroud)

但这会导入整个命名空间.现在我只想使用,比如说platform定义using common::platform

在Go中是否有相同的内容,所以我不必一直打字common.platform

c++ namespaces go

8
推荐指数
1
解决办法
1264
查看次数

如何使用Golang http.ResponseWriter将HTML字符串显示为网页?

我在第19章的例子中讲述了The Way to Go.

这是我的内容 main.go

package main

import (
    "fmt"
    "net/http"
)

const AddForm = `
<form method="POST" action="/add">
URL: <input type="text" name="url">
<input type="submit" value="Add">
</form>
`

func main() {
    http.HandleFunc("/add", Add)
    http.ListenAndServe(":8080", nil)
}

func Add(w http.ResponseWriter, r *http.Request) {
    url := r.FormValue("url")
    if url == "" {
        fmt.Fprint(w, AddForm)
        return
    }
    key := "Placeholder"
    fmt.Fprintf(w, "http://localhost:8080/%s", key)
}
Run Code Online (Sandbox Code Playgroud)

根据这本书及其截图,我应该看到在浏览器中呈现的有效表单.但是现在,我只能看到原始字符串.

我很新,所以我不确定自从编写示例以来语言是否已经发展了很多.我的版本是go version go1.6.2 linux/amd64,我认为这本书是在2012年用旧版本的go编写的.

如何修改它以使其在浏览器中呈现为表单?谢谢.

http go

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

如何从 Mac 访问 docker 容器 `localhost`?

请注意,这与如何公开在 docker 容器内运行的服务(绑定到 localhost )不同,后者可以在 Docker for Linux 中以多种方式解决,比如通过--net host甚至-v绑定我的 Linux 风格的客户端等。我的问题是特定的对于 Mac 版 Docker,所以事情没那么简单。

I have a TCP server binding to localhost:5005 running inside Docker for Mac. (For security reason, I must not bind to 0.0.0.0:5005.)

I have a TCP client sending request to this server from my Mac (not inside the docker container).

My question is, how do I make it work?

In Linux Docker, I would simply use --net=host so the …

macos docker docker-network

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

如何在go中打印正在运行的子进程的实时输出?

我有以下 bash 脚本bash_loop.sh,它打印 1 到 10 并在中间睡眠 3 秒。

\n\n
#!/bin/bash\n# Basic while loop\ncounter=1\nwhile [ $counter -le 10 ]\ndo\n    echo $counter\n    ((counter++))\n   sleep 3 \ndone\necho All done\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我的 go 代码如下:

\n\n
burstingScript := "bash_loop.sh"\ncmd := exec.Command("/bin/sh", burstingScript)\n\nvar out bytes.Buffer\ncmd.Stdout = &out\nif err := cmd.Run(); err != nil {\n    fmt.Println("An error has occurred..")\n    log.Fatal(err)\n}\nfmt.Println(out.String())\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,这只会打印出之后的所有内容,而不是在可用时打印内容。

\n\n

所以我的问题是,我是否可以打印每个数字打印每个数字,而不是在 bash 脚本完成执行后打印所有内容。

\n\n

PS1:在实际用例中,我必须实时处理 bash 脚本的输出,而不是简单地将内容打印到os.Stdout,所以我想知道是否有任何命令poll(),所以我想知道go 中

\n\n

PS2\xef\xbc\x9a在实际用例中,我想在发现有趣的消息后立即与子进程分离。例如,在读取 3 后,我希望函数立即返回 3,而不再等待其余输出,尽管我仍然希望子进程(bash …

go

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

如何使用标准库在Go中生成*唯一*随机数

问题:如何在Go中生成唯一的随机数流?

即,我想保证a使用math/rand和/或标准Go库实用程序的数组中没有重复。

func RandomNumberGenerator() *rand.Rand {
    s1 := rand.NewSource(time.Now().UnixNano())
    r1 := rand.New(s1)          
    return r1
}
rng := RandomNumberGenerator()    
N := 10000
for i := 0; i < N; i++ {
    a[i] = rng.Int()
}
Run Code Online (Sandbox Code Playgroud)

有关如何在Go中生成一系列随机数的问题和解决方案,例如,在此处

但是现在我想生成一系列随机数,这些随机数不会与以前的值重复。Go中是否有标准/推荐的方法?

我的猜测是(1)使用置换或(2)跟踪先前生成的数字并重新生成一个值(如果该值之前已生成)。

但是,如果我只想要几个数字,解决方案(1)听起来像是一个过大的杀伤力;如果由于碰撞而最终生成一连串的随机数,则解决方案(2)听起来很耗时,而且我猜想它也非常消耗内存。


用例:用没有重复的10K,100K,1M伪随机数对Go程序进行基准测试。

random go

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

makefile不适用于-std = c ++ 11选项

我正在尝试使用g ++ 4.8.2和以下makefile来使用一些C++ 11特性

CC=g++
DEBUG=-g
CFLAGS=-c -Wall -std=c++11 $(DEBUG)
LFLAGS = -Wall -std=c++11 $(DEBUG)
SOURCES=test.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LFLAGS) $(OBJECTS) -o $@ -std=c++11
.cpp .o:
    $(CC)  $(CFLAGS) $< -o $@ -std=c++11

clean:
    rm -rf *o $(EXECUTABLE)    
Run Code Online (Sandbox Code Playgroud)

但是当我打电话给"make"时,这是我得到的错误信息

$ make
g++    -c -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:18:15: error: range-based ‘for’ loops are not allowed in C++98 mode
  for (int i : {2, 3, 5, 7, 9, 13, 17, 19})
               ^
make: …
Run Code Online (Sandbox Code Playgroud)

c++ gcc makefile c++11

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

golang - 将枚举类型保存到SQL数据库"panic:sql:转换Exec参数#1的类型:从Value返回的非值类型int"

在我目前的go项目(~5K LOC)中,我使用sqlite3作为我的底层数据库层,我使用gorm作为我的ORM引擎.其中一个模型是Platform具有PlatformType枚举类型的字段.这是一个代码片段来演示我的问题.

package main

import (
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "github.com/jinzhu/gorm"
    "database/sql/driver"
    "fmt"
)

/****************************\
    Object Layer
\****************************/

// Platform ID 
type PlatformID string

func (u *PlatformID) Scan(value interface{}) error { *u = PlatformID(value.([]byte)); return nil }
func (u PlatformID) Value() (driver.Value, error)  { return string(u), nil }

// Platform Type enumeration
type PlatformType int
const (
    PLATFORM_TYPE_NOT_A_VALUE PlatformType = iota
    PLATFORM_TYPE_TYPE1
    PLATFORM_TYPE_TYPE2 
)

var types = [...]string {
    "Not a type",
    "Type1",
    "Type2",
}

func (platform_type PlatformType) …
Run Code Online (Sandbox Code Playgroud)

sqlite orm go

0
推荐指数
1
解决办法
3112
查看次数

标签 统计

go ×5

c++ ×2

c++11 ×1

docker ×1

docker-network ×1

gcc ×1

http ×1

macos ×1

makefile ×1

namespaces ×1

orm ×1

random ×1

sqlite ×1