这是对mmap文件的尝试并写入一个字节:
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
file, _ := os.Open("/tmp/data")
mmap, _ := syscall.Mmap(int(file.Fd()), 0, 100, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
fmt.Printf("cap is %d", cap(mmap))
mmap[0] = 0
syscall.Munmap(mmap)
}
Run Code Online (Sandbox Code Playgroud)
尽管长度设置为100,但mmap容量始终为0.系统调用出了什么问题?
我正在阅读Go Essentials:
Go中的字符串是字节的不可变序列(8位字节值),这与Python,C#,Java或Swift等语言(其中字符串为Unicode)不同。
我在玩以下代码:
s := "???"
b :=[]byte{0xe6, 0x97, 0xa5, 0xe6, 0x9c, 0xac, 0xe8, 0xaa, 0x9e}
fmt.Println(string(b) == s) // true
for i, runeChar := range b {
fmt.Printf("byte position %d: %#U\n", i, runeChar)
}
//byte position 0: U+00E6 'æ'
//byte position 1: U+0097
//byte position 2: U+00A5 '¥'
//byte position 3: U+00E6 'æ'
//byte position 4: U+009C
//byte position 5: U+00AC '¬'
//byte position 6: U+00E8 'è'
//byte position 7: U+00AA 'ª'
//byte position 8: U+009E …Run Code Online (Sandbox Code Playgroud) I'm trying to sort the characters in a string by sorting a slice of the bytes in the string (using sort.Slice). The code I'm using gets the right results sometimes but other times produces results I can't make sense of.
package main
import (
"fmt"
"sort"
)
func main() {
for _, s := range []string{"nat", "tan", "ant"} {
b := []byte(s)
sort.Slice(b, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(s, string(b))
}
}
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/bC9QWq7aF3G
I …
今天,我在构建go服务的管道中包括了“ go vet”。我想知道为什么go vet本地计算机上的输出与CI服务器上运行的输出不同。
我发现go版本有所不同-至少有点不同。我的本地Go版本是1.12.4,配置项是1.12.7。这个事实可以解释不同的行为,但是我不明白为什么会发生这种情况!
有气味:
type Something struct {
...
BatteryNumber string `json:"number"`
...
}
type SomethingWithBattery struct {
Something
Number string `json:"number"`
...
}
Run Code Online (Sandbox Code Playgroud)
因此,在struct标记中是“数字”的两倍,因为Something-struct嵌套SomethingWithBattery-1.12.4抱怨,而1.12.7则没有。为什么?
我使用生成的SQL脚本(sql 2k5)将列添加到表中.
我需要添加"检查是否存在",因为我的客户有时会运行脚本两次.(我无法控制这部分,这种情况一次又一次地发生)
我发现了一种加入sysobjects和syscolumns的方法,它可以工作.
我的问题是我必须在另一个表中添加一列,其中列不在表的末尾.
对于这个,SQL正在生成那么长的代码...用新列创建新的临时表,从旧表填充,删除旧表,最后重命名临时表.
这里的问题是这个脚本的脚本中有很多GO和事务...
我能做什么?
1.)删除所有GO - s?(不喜欢这个主意)
2.)在每个GO对之间添加我的IF?(不喜欢这个主意)
3.)是否有另一种有意义的方式,实施起来并不难
我无法想到任何事情,我可以检查发布版本,或者任何东西,而不仅仅是我的sysobjects和syscolumns join,但问题将是相同的.
因为GO-s,当它到达BEGIN的END时,我的If将被"遗忘"......
我一直在阅读[golang-book]:http://www.golang-book.com,并在我继续阅读时完成练习.在第6章中,有一个练习必须找到未排序列表中的最小元素[x].
我有以下代码但不知何故我不知道为什么方法长度(len)在第14行给出了一个错误:x.len undefined(type [] int没有字段或方法len)
package main
import "fmt"
func main() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
small := x[0]
for i := 1; i < x.len(); i++ {
if x[i] < small {
fmt.Println(x[i])
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用的逻辑是谷歌搜索所以也许在数组上没有len方法?任何帮助是极大的赞赏.
请参阅示例:http://play.golang.org/p/6d4uX15EOQ
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
c := "foofoofoofoofoofofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo"
fmt.Printf("c: %T, %d\n", c, unsafe.Sizeof(c))
fmt.Printf("c: %T, %d\n", c, reflect.TypeOf(c).Size())
}
Run Code Online (Sandbox Code Playgroud)
输出:
c: string, 8 //8 bytes?!
c: string, 8
Run Code Online (Sandbox Code Playgroud)
好像这么大的字符串不能有这么小的尺寸!出了什么问题?
我有以下代码,它将一个字符串作为输入转换为UNIX时间戳.我想在golang中做同样的事情,但我无法识别结构或函数,它将在Go中提供相当于DateTimeOffset的结构.
class Program
{
static void Main(string[] args)
{
var date = GetUtcTimestampFromAttribute();
Console.WriteLine(date);
if (date != null)
{
Console.WriteLine(ToUnixTimeStamp(date.Value));
}
Console.ReadKey();
}
public static DateTimeOffset? GetUtcTimestampFromAttribute()
{
var ticks = long.Parse("7036640000000");
Console.WriteLine(ticks);
return GetUtcTimestampFromTicks(ticks);
}
public static DateTimeOffset? GetUtcTimestampFromTicks(long ticks)
{
Console.WriteLine(new DateTimeOffset(ticks, TimeSpan.Zero));
return ticks != 0 ?
new DateTimeOffset(ticks, TimeSpan.Zero) :
(DateTimeOffset?)null;
}
public static long ToUnixTimeStamp(DateTimeOffset timeStamp)
{
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
return Convert.ToInt64((timeStamp - epoch).TotalSeconds);
}
}
Run Code Online (Sandbox Code Playgroud)
例如: …
package main
import "fmt"
type Employee struct {
ID int
Name string
}
func main(){
var zhexiao Employee
zhexiao.Name = "xiao"
fmt.Println(&zhexiao)
x := 1
p := &x
fmt.Println(p)
}
Run Code Online (Sandbox Code Playgroud)
上面的代码输出两种指针.
&{0 xiao}0xc0420600b0它看起来像一个内存地址)为什么struct指针输出不是内存地址?如果它不是内存地址,它是什么?
非常感谢
所以,我已经解决了我遇到的问题。这与我是否在 C# 客户端的 StreamWriter 上使用编码有关,但我想知道无论如何如何处理这些额外的 3 个字节。
这是一个用 C# 编写的客户端和一个用 Go 编写的服务器。为什么是 C#?稍后它将有用于云计算的 Unity 应用程序。为什么去?我只是想使用它。另外我的服务器是 Linux 并且 Go 很容易 x-compile。
问题是从我的 C# 客户端发送的数据在前面附加了 3 个额外的字节,这与 Go 的Json.Unmarshal函数在这些数据到达服务器后直接提供它相冲突。
这是离开 C# 客户端的 JSON 格式字符串
{"channel":0, "data": {"name":"Hasty Wombat","uuid":"e91ccc23-7e80-4189-958e-9b778dce1146","type":"Drone"}}\n
这是通过在 C# 客户端中配置为 UTF8 的流编写器之前的字节数组。
_sWriter = new StreamWriter(_tStream, System.Text.Encoding.UTF8, 8192);
长度:108
123 34 99 104 97 110 110 101 108 34 58 48 44 32 34 100 97 116 97 34 58 32 123 34 110 97 …Run Code Online (Sandbox Code Playgroud)