小编dmi*_*ova的帖子

如何在Go中针对用户输入编写测试

我如何测试来自fmt.Scan/Scanf/Scanln的用户输入?

例如,如何测试函数输入将从STDIN接受"4 5 \n"和"1 2 3 4 \n"并返回n == 5和array == [1,2,3,4].

package main

import (
    "fmt"
)

// input gets an array from the user.
func input() (m int, array []int) {
    fmt.Print("Enter the size of the array, n, and the difference, m: ")
    var n int
    _, err := fmt.Scanf("%d %d", &n, &m)
    if err != nil {
        panic(err)
    }

    fmt.Print("Enter the array as a space seperated string: ")
    array = make([]int, n)
    for i := 0; i …
Run Code Online (Sandbox Code Playgroud)

input go

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

以文本样式更改方程背景颜色

在Mathematica中,当您写入Text样式单元格时,如果创建格式化的等式,例如按"x ctrl_ a",则在选择等式时背景颜色会发生变化.有谁知道这个方程格式区域被调用,特别是在选择方程时如何更改背景颜色.

wolfram-mathematica mathematica-frontend

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

如何将Go地图序列化为protobuff

我正在学习教程,并参与了序列化/编组Go结构到协议缓冲区的部分.我的结构有一个地图,我找不到任何关于如何处理编组地图的文档.

在下面我想序列化Fields map[string]string:

结构:

type Note struct {
    ID     NoteID
    Fields map[string]string
}
Run Code Online (Sandbox Code Playgroud)

protobuf架构:

package internal;

message Note {
    optional int64 ID = 1;
    optional map<string, string> Fields = 2;
}
Run Code Online (Sandbox Code Playgroud)

去元帅:

func MarshalNote(n *remember.Note) ([]byte, error) {
    return proto.Marshal(&Note{
        ID: proto.Int64(int64(n.ID))
        Fields: proto.???
    })
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何处理最后一行以及我搜索关于将字段映射到protobuf方案的任何内容,而不是将映射映射到protobuf方案.

go protocol-buffers

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

如何在python中重新加载文件?

如果我有一个脚本将1000行写入文件然后继续针对该文件的正则表达式,但是只有最后100行文本可用.解决此问题的一种方法是关闭并重新打开文件.有没有办法在写入后重新加载文件,或者我应该只写一个写入关闭打开模块?在第一次打开时,日志文件不存在/为空可能是相关的.

>>> the_page = 'some large site opened through urllib'
>>> logfile = open('./artist/' + tags['ARTIST'], 'r+') 
>>> logfile.write(the_page)
>>> print logfile.read()
Run Code Online (Sandbox Code Playgroud)

什么也没出现.

>>> 'Some regular expressions search'
Run Code Online (Sandbox Code Playgroud)

搜索最后100行写入.

>>> logfile.close()
>>> logfile = open('./artist/' + tags['ARTIST'], 'r+') 
>>> print logfile.read()
Run Code Online (Sandbox Code Playgroud)

一切都出现了.

>>> 'Some regular expressions search'
Run Code Online (Sandbox Code Playgroud)

按预期执行.

python file-io

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

在Mathematica 8中将新单元格默认为1行

在Mathematica 8中,当您打开一个新单元格时,它默认为该单元格提供2行.我发现这非常烦人,并希望它只会打开1行.我搜索了一堆,检查了reference.wolfram.com,并查看了格式>选项检查器,没有运气.

wolfram-mathematica

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