[go-nuts] Golang SQL query Errror: missing destination name Sum in *[]main.Tag

2022-07-15 Thread Harry Butler


I’m trying to feed the results from the SQL Query into the Sum field of my 
struct however i’m met with this error despite their being a Sum Field in 
the struct.

The results from the query can range from 1-8 numbers depending on the date 
input in my REACT interface.

package main

import (
“encoding/json”
“log”
“net/http”
“time”
_ "github.com/go-sql-driver/mysql" 
"github.com/jmoiron/sqlx" 

)

type Tag struct {
Sum string json:"Sum"
Stream_count string json:"stream_count"
Query_desc string json:"Query_Desc"
Query_start_date string json:"Query_start_date"
Query_end_date string json:"Query_end_date"
Current_date string json:"Current_date"
Error_info string json:"Error_Info"
}

func handler(w http.ResponseWriter, r 
*http.Request) { w.Header().Set(“Access-Control-Allow-Origin”, "*")
w.Header().Set(“Access-Control-Allow-Headers”, “Origin, X-Requested-With, 
Content-Type, Accept”)
db, err := sqlx.Connect("mysql", "")

date_1 := r.FormValue("date_1") 
date_2 := r.FormValue("date_2") 
time_1 := r.FormValue("time_1") 
time_2 := r.FormValue("time_2") 

var tag Tag 

Query := ("SELECT SUM(duration) Sum FROM sessions WHERE (app_id = '' OR 
app_id ='' OR app_id ='' OR app_id =''OR app_id ='') AND 
date(created ) between ? and ? and time(created ) between ? and ? AND 
(`media_src`='https://livestream.indemandradio.com/stream/index.m3u8' OR 
`media_src`='' OR `media_src`='' OR `media_src`='' OR 
`media_src`='' OR `media_src`='') GROUP BY date(created)") err = 
db.Get(, Query, date_1, date_2, time_1, time_2) log.Print(tag.Sum) 
tag.Query_desc = "Listener Hours" tag.Query_start_date = date_1 
tag.Query_end_date = date_2 dt := time.Now() tag.Current_date = 
dt.Format("01-02-2006 15:04:05") if err != nil {tag.Error_info = 
err.Error() } j, err := json.Marshal(tag) if err != nil {   
 w.WriteHeader(http.StatusBadRequest)w.Write([]byte((err.Error(   
 return } w.Write(j) 

}

func main() {
http.HandleFunc(“/”, handler)
log.Fatal(http.ListenAndServe(“:8081”, nil))
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/38f01c42-8afb-476c-bce8-66466edb806fn%40googlegroups.com.


Re: [go-nuts] [ERROR] invalid argument when executing bufio.NewWriter.Flush()

2017-06-03 Thread Harry
Thanks Jan!

You are great!

On Saturday, 3 June 2017 13:00:51 UTC+2, Jan Mercl wrote:
>
> On Sat, Jun 3, 2017 at 12:42 PM Harry <hiro...@gmail.com > 
> wrote:
>
> > How can I handle to write something out using bufio.NewWriter?
>
> Just fine. The problem is you haven't created any file to write to in the 
> first place, which checking the error could have told you.
>
> -- 
>
> -j
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] [ERROR] invalid argument when executing bufio.NewWriter.Flush()

2017-06-03 Thread Harry
Hi guys,

How can I handle to write something out using bufio.NewWriter?

My code is like below,

var writer *bufio.Writer
var err error
file, _ := os.OpenFile(*fileName, os.O_WRONLY | os.O_APPEND, 0644)
defer file.Close()

writer = bufio.NewWriter(file)

writer.WriteString(strings.Join("something\n")

err = writer.Flush()
if err != nil {
//invalid argument
log.Fatal(err)
}


What is wrong?


Thanks guys!!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Puzzled by a profiling result around allocations

2017-03-20 Thread Harry B
Hi,

As part of preparing an internal go talk to explain using strings vs bytes 
of the bufio.Scanner, I created two small samples 
https://play.golang.org/p/-TYycdHaPC and 
https://play.golang.org/p/L7W-jiaHdL , the only difference in the hot path 
is reducing the conversions from `[]byte` to `string` (later also avoids 
fmt module’s string conversions). Input being a 10 million line file. Match 
is only one line.

For the moment, ignore the fact that I am not checking for number of 
arguments, is doing unnecessary optimizations etc. I am trying to take a 
simple case to explain.

First version runs in 3 seconds, second in 2 seconds. For the slower 
version using `.Text()` 90+ percent of the time was in `syscall.Syscall` 
(under syscall.Read on the file) that I wasn’t expecting anything more than 
10% change. However, the second version shows a much bigger reduction in 
`syscall.Syscall`. How is that possible? I expected it to be faster, but 
not affect the time spend reading file.The exclusion of fmt was unnecessary 
since the number of matches will be very small. But that is probably not 
relevant here. I have run it multiple times and made sure file is cached.

Attached are the profile SVGs. You can also see that an entire branch of 
the execution has disappeared in the faster/second version.

I do understand the second has avoided the allocation of string via 
scanner.Text(), however, the output of  go build -gcflags "-m"  give me the 
following

// First version

> go build -gcflags "-m" grep_simple_re.go
# command-line-arguments
./grep_simple_re.go:17: inlining call to bufio.NewScanner
./grep_simple_re.go:19: inlining call to (*bufio.Scanner).Text
./grep_simple_re.go:17: fp escapes to heap
./grep_simple_re.go:19: string(bufio.s·2.token) escapes to heap
*./grep_simple_re.go:21: line escapes to heap*
./grep_simple_re.go:17: main  literal does not escape
./grep_simple_re.go:21: main ... argument does not escape
:1: leaking param: io.p
:1: leaking param: .this

// Second version
> go build -gcflags "-m" grep_simple_re_bytes.go
# command-line-arguments
./grep_simple_re_bytes.go:18: inlining call to bufio.NewScanner
./grep_simple_re_bytes.go:20: inlining call to (*bufio.Scanner).Bytes
./grep_simple_re_bytes.go:16: fn escapes to heap
./grep_simple_re_bytes.go:16: err escapes to heap
./grep_simple_re_bytes.go:18: fp escapes to heap
./grep_simple_re_bytes.go:16: main ... argument does not escape
./grep_simple_re_bytes.go:18: main  literal does not escape
./grep_simple_re_bytes.go:24: main ([]byte)("\n") does not escape
:1: leaking param: io.p
:1: leaking param: .this


*I can see "./grep_simple_re.go:21: line escapes to heap" could be a 
problem in the first version.*

Here is the memory profiles of a run on 10million line 2.4 G file

$ go tool pprof ./grep_simple_re_mprofile 
/var/folders/bt/1mh2p2vx41lbnq3fz1n8qlxwgn/T/profile066323621/mem.pprof
Entering interactive mode (type "help" for commands)
(pprof) top10
86.37kB of 86.37kB total (  100%)
Dropped 4 nodes (cum <= 0.43kB)
Showing top 10 nodes out of 23 (cum >= 43.89kB)
  flat  flat%   sum%cum   cum%
   39.73kB 46.00% 46.00%39.73kB 46.00%  regexp.(*bitState).reset
   16.30kB 18.87% 64.87%16.30kB 18.87%  bufio.(*Scanner).Scan
   12.62kB 14.61% 79.48%12.62kB 14.61%  runtime.malg
9.04kB 10.47% 89.95%17.45kB 20.21%  runtime.allocm
4.52kB  5.23% 95.19% 4.52kB  5.23%  runtime.rawstringtmp
4.16kB  4.81%   100% 4.16kB  4.81%  regexp.progMachine
 0 0%   100%64.71kB 74.92%  main.main
 0 0%   100%43.89kB 50.82%  regexp.(*Regexp).MatchString
 0 0%   100%43.89kB 50.82%  regexp.(*Regexp).doExecute
 0 0%   100%43.89kB 50.82%  regexp.(*Regexp).doMatch
(pprof) quit
$ go tool pprof* -alloc_space* ./grep_simple_re_mprofile 
/var/folders/bt/1mh2p2vx41lbnq3fz1n8qlxwgn/T/profile066323621/mem.pprof
Entering interactive mode (type "help" for commands)
(pprof) top10
2.14GB of 2.14GB total (  100%)
Dropped 22 nodes (cum <= 0.01GB)
  flat  flat%   sum%cum   cum%
2.14GB   100%   100% 2.14GB   100%  runtime.rawstringtmp
 0 0%   100% 2.14GB   100%  main.main
 0 0%   100% 2.14GB   100%  runtime.goexit
 0 0%   100% 2.14GB   100%  runtime.main
 0 0%   100% 2.14GB   100%  runtime.slicebytetostring


*The only explanation for such a large change is if it grew the stack 2.4G 
in size for the first case. The SVG attached seems to show something like 
that. Instead if it was heap allocation, why didn't it show up in the first 
pprof (memory profile) output above? Does 'escape to heap' not imply it 
will be allocated in heap?Any call to re.Match or fmt.Println() escapes the 
string?*

 System details

```
go version go1.8 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOS

Re: [go-nuts] Why result type is different on sql.Query()

2016-09-28 Thread Harry
Thanks, Konstantin.

It would be nice idea,
I'll try to check it!

2016年9月28日水曜日 23時00分14秒 UTC+9 Konstantin Khomoutov:
>
> On Wed, 28 Sep 2016 06:30:05 -0700 (PDT) 
> Harry <hiro...@gmail.com > wrote: 
>
> > > In a nutshell, Scan() internally performs several nested type 
> > > switches based on the type of the Scan destination and what is 
> > > received from the database (in other words, the driver). 
> > > 
> > > As to why the two scans returned values of different types, more 
> > > information is needed. But since the queries were similar and the 
> > > dest were the same, you could start by looking at what is stored in 
> > > the database and what's the int64/[]byte returned. 
> > > 
> > These two SQL expect same result. 
> > 
> > Field1's type on MySQL is int. 
> > When returning value as string type, that value is same to int value 
> > after asserted to int. 
> > But it's troublesome to handle two different result by placeholder 
> > added/not added. 
>
> I would take tcpdump / Wireshark, capture the exchange with the server 
> in both cases and look at what Wireshark's dissectors decode from that 
> exchange in both cases.  If there's some information delivered by the 
> server along with its response (it should, I'd think), you'll be able 
> to see how it differs.  Alternatively, you should be able to tell any 
> difference between how the MySQL driver on the Go side handles the 
> query in both the cases.  This one is interesting.  Say, the driver 
> might actually prepare the query behind your back.  Or it may do 
> something weird when escaping the argument when constructing the full 
> query string if it's not using prepared statements. 
>
> To recap, there are at least two points of interest to look at. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Why result type is different on sql.Query()

2016-09-28 Thread Harry
Thank you ksug,
Your indication makes sense.

I try to check more detail.
And I'll feedback.

2016年9月28日水曜日 22時58分24秒 UTC+9 ksug:
>
> I don't think placeholder vs no placeholder makes a difference. Here is 
> an example where Scan() into the same table could return values of 
> different types (using sqlite instead of mysql). 
>
> https://play.golang.org/p/rAA04pv_I_ 
>
> I'm not saying that this is what's definitely happening on your end, 
> just that it's possible. 
> Look at: 
> - implicit type conversions 
> - database, driver behavior 
> - driver bug 
> - etc 
>
> As I said, more information is needed. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Why result type is different on sql.Query()

2016-09-28 Thread Harry
Thanks ksug.

These two SQL expect same result.

Field1's type on MySQL is int.
When returning value as string type, that value is same to int value after 
asserted to int.
But it's troublesome to handle two different result by placeholder 
added/not added.


2016年9月28日水曜日 21時23分52秒 UTC+9 ksug:
>
> In a nutshell, Scan() internally performs several nested type switches 
> based on the type of the Scan destination and what is received from the 
> database (in other words, the driver). 
>
> As to why the two scans returned values of different types, more 
> information is needed. But since the queries were similar and the dest 
> were the same, you could start by looking at what is stored in the 
> database and what's the int64/[]byte returned. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Why result type is different on sql.Query()

2016-09-28 Thread Harry
I made a mistake.

Second query don't include placeholder.

sql := "SELECT field1, field2 FROM t_xxx"
rows1, err := ms.DB.Query(sql)

There is no error.
I'm asking why result type was changed/


2016年9月28日水曜日 19時30分23秒 UTC+9 ksug:
>
> Did you check your error after each call to Query()? 
> Without an arg, what do you expect the "?" in "flg=?" to be formatted 
> into? 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Why result type is different on sql.Query()

2016-09-27 Thread Harry
I mention about the below func of ```database/sql``` package and I'm using 
golang 1.7.

func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {...}


I'm using MySQL and handling query result like that code below, (there are 
some omissions.)


sql := "SELECT field1, field2 FROM t_xxx WHERE flg=?"
//1-1) added args parameter 
rows, err := ms.DB.Query(sql, 1)

values := make([]interface{}, 2)
scanArgs := make([]interface{}, 2)
for i := range values {
 scanArgs[i] = [i]
}

err = rows.Scan(scanArgs...)

//1-2) I expect that type of field1 is int64
val := reflect.ValueOf(values[0])
//val.Kind()==reflect.Int64


//--
//Next check
//--

//2-1) no args parameter 
rows1, err := ms.DB.Query(sql)

values2 := make([]interface{}, 2)
scanArgs2 := make([]interface{}, 2)
for i := range values2 {
 scanArgs2[i] = [i]
}

err = rows2.Scan(scanArgs2...)

//2-2) I expect that type of field1 is int64
val := reflect.ValueOf(values2[0])
//But, result was not int, it was byte. and it can be asseted as string.
//val.Kind()==reflect.Slice ([]uint8)


When adding args to Query func, result type is OK, but without it, result 
type change into []byte.

Why, result type changed???



Thank you.

Harry

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: How example can be run in test?

2016-09-17 Thread Harry
I got it.
In my case, there was only Example func.
When adding something Testxxx func, it worked well.


2016年9月17日土曜日 6時52分30秒 UTC+9 James Bardin:
>
> This is fixed in master. In go1.7 the Examples and Benchmarks aren't run 
> if there are no Test functions. 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How can I get running func name?

2016-07-13 Thread Harry
I want to keep something records as log and where it is to specify.

2016年7月13日水曜日 21時28分52秒 UTC+9 Konstantin Khomoutov:
>
> On Wed, 13 Jul 2016 04:48:18 -0700 (PDT) 
> Harry <hiro...@gmail.com > wrote: 
>
> > func SomethingFunc() { 
> >   fmt.Printf("func is %s\n", funcName) 
> >   /* code */ 
> > } 
> > 
> > I want to show func name when running each func. 
> > If I could get func name automatically, same code can be used. 
>
> What problem are you trying to solve with this? 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] How can I get running func name?

2016-07-13 Thread Harry
Hello, everyone.

As title, I'm trying the below.

func SomethingFunc() {
  fmt.Printf("func is %s\n", funcName)
  /* code */
}

I want to show func name when running each func.
If I could get func name automatically, same code can be used.


Thank you.

Harry.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Result is sometimes different despite using same value when encode by encoding/gob

2016-06-21 Thread Harry
Thank Dave and Jan.

I understood completely.
I'd consider another solution to manage that next.

Thanks again.


Harry.


2016年6月21日火曜日 14時58分39秒 UTC+9 Dave Cheney:
>
> To serialise the keys and values of a map and code would have to iterate 
> over it
>
> for k,v := range m { 
>// serialise k and v
> }
>
> But map iteration does not have a guaranteed ordering. This is why the 
> result is not stable.
>
> On Tuesday, 21 June 2016 15:44:05 UTC+10, Harry wrote:
>>
>> Hello guys,
>>
>> I'm trying to use encoding/gob package to know how it works.
>> And I wrote below codes.
>>
>>
>>
>> import "encoding/gob"
>>
>>
>> func ToGOB64(data interface{}) (string, error) {
>>  b := bytes.Buffer{}
>>
>>  e := gob.NewEncoder()
>>  err := e.Encode(data)
>>  if err != nil {
>>  fmt.Println(`failed gob Encode`, err)
>>  return "", err
>>  }
>>  return base64.StdEncoding.EncodeToString(b.Bytes()), nil
>>
>> }
>>
>>
>>
>> func FromGOB64(str string, tData interface{}) error {
>>
>>  by, err := base64.StdEncoding.DecodeString(str)
>>
>>  if err != nil {
>>  return err
>>
>>  }
>>  b := bytes.Buffer{}
>>  b.Write(by)
>>  d := gob.NewDecoder()
>>  err = d.Decode(tData)
>>  if err != nil {
>>  return err
>>
>>  }
>>  return nil
>> }
>>
>>
>> And then, I prepared for test targeted type are struct, map, interface{}.
>>
>> func TestSerializeStruct(t *testing.T) {
>>  u := User{Id: 10, Name: "harry dayo"}
>>
>>  result, err := ToGOB64(u)
>>  if err != nil {
>>  t.Errorf("TestSerializeStruct error: %s", err)
>>  }
>>  if result != 
>> "Iv+BAwEBBFVzZXIB/4IAAQIBAklkAQQAAQROYW1lAQwR/4IBFAEKaGFycnkgZGF5bwA=" 
>> {
>>  t.Errorf("TestSerializeStruct result: %+v", result)
>>  }
>> }
>>
>>
>>
>> func TestDeSerializeStruct(t *testing.T) {
>>
>>
>>  u := User{}
>>  err := FromGOB64(
>> "Iv+BAwEBBFVzZXIB/4IAAQIBAklkAQQAAQROYW1lAQwR/4IBFAEKaGFycnkgZGF5bwA="
>> , )
>>  if err != nil {
>>  t.Errorf("TestDeSerializeStruct error: %s", err)
>>  }
>>  if u.Id != 10 {
>>  t.Errorf("TestDeSerializeStruct result: %+v", u)
>>  }
>> }
>>
>>
>> //2.map (map is not stable.)
>> func TestSerializeMap(t *testing.T) {
>>
>>
>>  m := map[string]int{"apple": 150, "banana": 300, "lemon": 300}
>>  //when using map, result is not stable.
>>  result, err := ToGOB64(m)
>>  if err != nil {
>>  t.Errorf("TestSerializeMap error: %s", err)
>>  }
>>  if result != 
>> "Dv+DBAEC/4QAAQwBBAAAIP+EAAMFYXBwbGX+ASwGYmFuYW5h/gJYBWxlbW9u/gJY" {
>>  //if result != 
>> "Dv+DBAEC/4QAAQwBBAAAIP+EAAMGYmFuYW5h/gJYBWxlbW9u/gJYBWFwcGxl/gEs" {
>>  t.Errorf("TestSerializeMap result: %#v", result)
>>  }
>> }
>>
>>
>> //
>> func TestDeSerializeMap(t *testing.T) {
>>
>>
>>  u := map[string]int{}
>>  
>> //FromGOB64("Dv+DBAEC/4QAAQwBBAAAIP+EAAMFYXBwbGX+ASwGYmFuYW5h/gJYBWxlbW9u/gJY",
>>  
>> )
>>  err := FromGOB64(
>> "Dv+DBAEC/4QAAQwBBAAAIP+EAAMGYmFuYW5h/gJYBWxlbW9u/gJYBWFwcGxl/gEs", )
>>  if err != nil {
>>  t.Errorf("TestDeSerializeMap error: %s", err)
>>  }
>>  if u["apple"] != 150 {
>>  t.Errorf("TestDeSerializeMap result: %#v", u)
>>  }
>> }
>>
>>
>>
>> //interface x slice
>> func TestSerializeInterfaces(t *testing.T) {
>>
>>
>>  args := []interface{}{1, "abcde", true}
>>  result, err := ToGOB64(args)
>>  if err != nil {
>>  t.Errorf("TestSerializeInterfaces error: %s", err)
>>  }
>>  if result != 
>> "DP+BAgEC/4IAARAAACX/ggADA2ludAQCAAIGc3RyaW5nDAcABWFiY2RlBGJvb2wCAgAB" {
>>  t.Errorf("TestSerializeInterfaces result: %+v", result)
>>  }
>> }
>>
>>
>>
>> func TestDeSerializeInterfaces(t *testing.T) {
>>
>>
>>  args := []interface{}{}
>>  err := FromGOB64(
>> "DP+BAgEC/4IAARAAACX/ggADA2ludAQCAAIGc3RyaW5nDAcABWFiY2RlBGJvb2wCAgAB", &
>> args)
>>  if err != nil {
>>  t.Errorf("TestDeSerializeInterfaces error: %s", err)
>>  }
>>  if args[0] != 1 || args[1] != "abcde" || args[2] != true {
>>  t.Errorf("TestDeSerializeInterfaces result: %+v", args)
>>  }
>> }
>>
>>
>>
>> As a result, serialized map value often change though deserialized map is 
>> no problem.
>> Why serialized value is not invaliable when using map value.
>>
>> I'd like to know that reason sincerely.
>>
>> Thanks in advance.
>>
>>
>>
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Result is sometimes different despite using same value when encode by encoding/gob

2016-06-20 Thread Harry
Hello guys,

I'm trying to use encoding/gob package to know how it works.
And I wrote below codes.



import "encoding/gob"


func ToGOB64(data interface{}) (string, error) {
 b := bytes.Buffer{}

 e := gob.NewEncoder()
 err := e.Encode(data)
 if err != nil {
 fmt.Println(`failed gob Encode`, err)
 return "", err
 }
 return base64.StdEncoding.EncodeToString(b.Bytes()), nil

}



func FromGOB64(str string, tData interface{}) error {

 by, err := base64.StdEncoding.DecodeString(str)

 if err != nil {
 return err

 }
 b := bytes.Buffer{}
 b.Write(by)
 d := gob.NewDecoder()
 err = d.Decode(tData)
 if err != nil {
 return err

 }
 return nil
}


And then, I prepared for test targeted type are struct, map, interface{}.

func TestSerializeStruct(t *testing.T) {
 u := User{Id: 10, Name: "harry dayo"}

 result, err := ToGOB64(u)
 if err != nil {
 t.Errorf("TestSerializeStruct error: %s", err)
 }
 if result != 
"Iv+BAwEBBFVzZXIB/4IAAQIBAklkAQQAAQROYW1lAQwR/4IBFAEKaGFycnkgZGF5bwA=" {
 t.Errorf("TestSerializeStruct result: %+v", result)
 }
}



func TestDeSerializeStruct(t *testing.T) {


 u := User{}
 err := FromGOB64(
"Iv+BAwEBBFVzZXIB/4IAAQIBAklkAQQAAQROYW1lAQwR/4IBFAEKaGFycnkgZGF5bwA=", 
)
 if err != nil {
 t.Errorf("TestDeSerializeStruct error: %s", err)
 }
 if u.Id != 10 {
 t.Errorf("TestDeSerializeStruct result: %+v", u)
 }
}


//2.map (map is not stable.)
func TestSerializeMap(t *testing.T) {


 m := map[string]int{"apple": 150, "banana": 300, "lemon": 300}
 //when using map, result is not stable.
 result, err := ToGOB64(m)
 if err != nil {
 t.Errorf("TestSerializeMap error: %s", err)
 }
 if result != 
"Dv+DBAEC/4QAAQwBBAAAIP+EAAMFYXBwbGX+ASwGYmFuYW5h/gJYBWxlbW9u/gJY" {
 //if result != 
"Dv+DBAEC/4QAAQwBBAAAIP+EAAMGYmFuYW5h/gJYBWxlbW9u/gJYBWFwcGxl/gEs" {
 t.Errorf("TestSerializeMap result: %#v", result)
 }
}


//
func TestDeSerializeMap(t *testing.T) {


 u := map[string]int{}
 
//FromGOB64("Dv+DBAEC/4QAAQwBBAAAIP+EAAMFYXBwbGX+ASwGYmFuYW5h/gJYBWxlbW9u/gJY", 
)
 err := FromGOB64(
"Dv+DBAEC/4QAAQwBBAAAIP+EAAMGYmFuYW5h/gJYBWxlbW9u/gJYBWFwcGxl/gEs", )
 if err != nil {
 t.Errorf("TestDeSerializeMap error: %s", err)
 }
 if u["apple"] != 150 {
 t.Errorf("TestDeSerializeMap result: %#v", u)
 }
}



//interface x slice
func TestSerializeInterfaces(t *testing.T) {


 args := []interface{}{1, "abcde", true}
 result, err := ToGOB64(args)
 if err != nil {
 t.Errorf("TestSerializeInterfaces error: %s", err)
 }
 if result != 
"DP+BAgEC/4IAARAAACX/ggADA2ludAQCAAIGc3RyaW5nDAcABWFiY2RlBGJvb2wCAgAB" {
 t.Errorf("TestSerializeInterfaces result: %+v", result)
 }
}



func TestDeSerializeInterfaces(t *testing.T) {


 args := []interface{}{}
 err := FromGOB64(
"DP+BAgEC/4IAARAAACX/ggADA2ludAQCAAIGc3RyaW5nDAcABWFiY2RlBGJvb2wCAgAB", &
args)
 if err != nil {
 t.Errorf("TestDeSerializeInterfaces error: %s", err)
 }
 if args[0] != 1 || args[1] != "abcde" || args[2] != true {
 t.Errorf("TestDeSerializeInterfaces result: %+v", args)
 }
}



As a result, serialized map value often change though deserialized map is 
no problem.
Why serialized value is not invaliable when using map value.

I'd like to know that reason sincerely.

Thanks in advance.





-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: How to get executed query string after execution.

2016-06-13 Thread Harry
Hi Dave,

Now, I'm developping cache system of query result to redis or another 
key-value store.
Stored data are supposed that seriarized result of query using key 
serialized query string exected actually.


Harry

2016年6月14日火曜日 10時23分25秒 UTC+9 Dave Cheney:
>
> If there was a way to get the contents of the query string, what would you 
> do with that information?
>
> On Tuesday, 14 June 2016 11:18:47 UTC+10, Harry wrote:
>>
>> Hi guys,
>>
>> I wonder that question.
>> Before execution, it's possible to get query by myself.
>> But I want to get executed query string actually.
>> Is there any way to manage that?
>>
>> Thanks!
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.