• c++如何调用go静态库 at 2016年12月16日

    可以的,刚才试了一下。参照这个 http://blog.ralch.com/tutorial/golang-sharing-libraries/ 生成的 so 文件比较大,2MB 左右

    //main.go
    package main
    
    import "C"
    import "fmt"
    
    //export SayHello
    func SayHello(name string) {
        fmt.Printf("Nautilus says: Hello, %s!\n", name)
    }
    
    //export SayBye
    func SayBye() {
        fmt.Println("Nautilus says: Bye!")
    }
    
    func main() {
        // We need the main function to make possible
        // CGO compiler to compile the package as C shared library
    }
    
    // test.cpp
    #include "testso.h"
    #include <iostream>
    
    int main() {
        std::cout << "This is a Cpp Application." << std::endl;
    
        GoString name = {"Jack", 4};
        SayHello(name);
        SayBye();
    
        return 0;
    }
    
    // compile
    g++ -o app test.cpp testso.h -L${DIR} -ltestso
    
    // run
    ./app
    This is a Cpp Application.
    Nautilus says: Hello, Jack!
    Nautilus says: Bye!
    
    
  • golang slice的问题 at 2016年12月15日

    https://github.com/golang/go/blob/272032d0b245635ca0c5ca4c22e64496174b2ae3/src/runtime/malloc.go#L543

    func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
        if gcphase == _GCmarktermination {
            throw("mallocgc called with gcphase == _GCmarktermination")
        }
    
        if size == 0 {
            return unsafe.Pointer(&zerobase)
        }
    
  • 协程无法启动 at 2016年10月21日

    因为主协程退出了。最后 time.Sleep(time.Second) 一下看看。

  • 根据 StringHeader 结构,尝试了下手动获取 Len 的值,感觉好麻烦

    type StringHeader struct {
        Data uintptr
        Len  int
    }
    
    s := "testgo"
    sHeader := (*reflect.StringHeader)(unsafe.Pointer(&s))
    fmt.Printf("ptr is %p %p, %p \n", &s, sHeader, &sHeader.Data)
    l := (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sHeader)) + 8))
    fmt.Println("len is", sHeader.Len, *l)
    
  • 发现了,还真是

    go test -benchmem -bench=StringByte -run=xxx
    Benchmark_StringByte-4              500000000            3.43 ns/op        0 B/op          0 allocs/op
    Benchmark_StringByte_Simple-4       50000000            31.7 ns/op         8 B/op          1 allocs/op
    Benchmark_StringByte_Quick-4        1000000000           2.07 ns/op        0 B/op          0 allocs/op