Skip to content

Instantly share code, notes, and snippets.

View helinwang's full-sized avatar

Helin Wang helinwang

  • Bay Area
View GitHub Profile
@helinwang
helinwang / gostring.md
Last active December 2, 2024 10:11
Call go (or c) code from python with string as argument and string as return value

go code (foo.go) compiled into a shared library and a c header (foo.h) is generated.

For calling go from c, please see here

Arguments

The code below shows two ways of passing string parameter to go function:

  1. Using GoString structure as argument, without making a copy in go code: no conversion to go string needed.
  2. Using c_char_p as argument, making a copy in go code when converting to go string.

When using the first method without the copy, I don't know how python will do the memory management with the pointer passed into go. So the second method is preferred.

@helinwang
helinwang / gist.go
Last active December 11, 2016 16:27
save and load tensorflow tensors using tensorflow golang api
// Save:
outTensor, err := tf.NewTensor(filename)
if err != nil {
return err
}
_, err = session.Run(
map[tf.Output]*tf.Tensor{
graph.Operation("save/Const").Output(0): outTensor,
},
nil,
@helinwang
helinwang / tf_idf.go
Last active January 27, 2016 08:14
calculate max tfidf of each word in text corpus (given directory) in golang
package main
import (
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"sort"
"strings"
@helinwang
helinwang / gist:ea9a2e88f20278d2cb53
Created June 17, 2014 13:49
golang https server
package main
import (
"net/http"
"fmt"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Inside handler")
fmt.Fprintf(w, "Hello world from my Go program!")