[go-nuts] Re: Where should interfaces live?

2016-11-22 Thread Egon
On Monday, 21 November 2016 17:30:21 UTC+2, Vorn Mom wrote:

> Sorry if it was asked before, but where should interfaces live?
>
>- In the package that contains an implementation of it.
>- In its own package.
>- or in the package that needs it.
>
> The quick answer here is "yes to all", but most commonly it should be in 
"the package that needs it".

The interface can be seen as an evolution of indirection:

*#1 - no interface*

type memorydb struct { people []*Person }
func (db *memorydb) PersonByName(name string) (*Person, error) { ... }

type Server struct {
db *memorydb
}

*#2 - private interface*, introduced to allow multiple db or to separate 
code more

type memorydb struct { ... }
...
type sqllitedb struct { ... }
...

type peopledb interface { PersonByName(name string) (*Person, error) }

type Server struct {
db peopledb
}

*#3 - public interface* - introduced to allow sub-packages access/see the 
same interface, this usually happens together with moving the 
memorydb/sqllitedb into a separate package. It can also be used to break 
dependency cycles.

...
type PeopleDB interface { PersonByName(name string) (*Person, error) }

type Server struct {
db PeopleDB
}

*#4 - common interface* - introduced to allow wide compatibility between 
things; you only end-up here when you are implementing a widely used 
package, e.g. "database/sql/driver.Driver", "io.Reader", "io.Writer", 
"context.Context".

Try to stop at the "smallest number" that solves your problem. Each step 
adds extra indirection and more artifacts, so making code harder to 
understand. Of course, you should keep in mind not to make each 
file/struct/package/interface too large, which also makes code harder to 
read. It's a balancing act . Most web applications will end up at #3... 
frameworks/libraries end up at #3 or #4. Anything else can vary from #1 - 
#4.

+ Egon

The standard library is not consistent.  For example:
>
>- io.Writer is defined in a package that also has an implementation, 
>io.PipeWriter.  
>- Encoding is all interfaces, defering implementation to sub-packages.
>- database/sql returns a DB struct on Open() instead of an interface.
>
> Because Go interfaces are decoupled from implementation, I think it should 
> be defined where it's needed.  So I like having my own interface for DB 
> that may only define a subset of what behaviors it provides.
>
> However, doesn't defining an interface that depends on other packages 
> decrease cohesion and leaks the abstraction?  For example,
>
> type interface DbQuery {
>   QueryRow(query string, args ...interface{}) *sql.Row
> }
>
> This interface depends on sql.Row.  This decreases cohesion because the 
> interface is now defined across packages?
>
> -Vorn
>
>
>

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
Please try profiling your application, if you are on Linux perf(1) works very 
well for tracing user and system time.

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Cia


On Tuesday, 22 November 2016 17:46:18 UTC+8, Dave Cheney wrote:
>
> Please try profiling your application, if you are on Linux perf(1) works 
> very well for tracing user and system time.


Thanks for mention that :)

Here is the first step result of *perf stat*: 


 Performance counter stats for './penalty_with_LockOSThread':

   4893.495113  task-clock (msec) #0.583 CPUs utilized
 *2,112,910  context-switches  #0.432 M/sec*
80  cpu-migrations#0.016 K/sec
 2,377  page-faults   #0.486 K/sec
 cycles
 stalled-cycles-frontend
 stalled-cycles-backend
 instructions
 branches
 branch-misses

   8.389103849 seconds time elapsed

 Performance counter stats for './penalty_without_LockOSThread':

930.442000  task-clock (msec) #0.978 CPUs utilized
 2,947  context-switches  #0.003 M/sec
41  cpu-migrations#0.044 K/sec
 2,389  page-faults   #0.003 M/sec
 cycles
 stalled-cycles-frontend
 stalled-cycles-backend
 instructions
 branches
 branch-misses

   0.951572314 seconds time elapsed


It seems there is something wrong with the *context-switches *for 
penalty_without_LockOSThread at first sight.

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
Perf record / report will highlight the problem.

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
Related, you don't need a pointer to a chan, channels are already pointers to 
the private runtime channel type. 

-- 
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: Reflect Struct Comparison

2016-11-22 Thread Dave Cheney
Please do not reopen to a four year old thread. Instead please start a new 
thread describing the problem you have, what you tried, and what happened when 
you tried.

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.


[go-nuts] Re: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Cia


On Tuesday, 22 November 2016 18:06:50 UTC+8, Dave Cheney wrote:
>
> Perf record / report will highlight the problem.


Thanks Dave.And here is the output of the perf report:

Samples: 48K of event 'cpu-clock', Event count (approx.): 1202025
Overhead  Command  Shared Object  Symbol
  59.40%  penalty  [kernel.kallsyms]  [k] _raw_spin_unlock_irqrestore
  13.22%  penalty  [kernel.kallsyms]  [k] finish_task_switch
   2.13%  penalty  [kernel.kallsyms]  [k] get_user_pages_fast
   1.87%  penalty  penalty[.] runtime.mallocgc
   1.05%  penalty  [kernel.kallsyms]  [k] futex_wake
   0.75%  penalty  penalty[.] runtime.findrunnable
   0.72%  penalty  penalty[.] runtime.futex
   0.71%  penalty  penalty[.] runtime/internal/atomic.Xchg
   0.67%  penalty  [kernel.kallsyms]  [k] unlock_page
   0.61%  penalty  [kernel.kallsyms]  [k] wake_q_add
   0.56%  penalty  penalty[.] runtime.schedule
   0.55%  penalty  penalty[.] runtime.runqgrab
   0.52%  penalty  [kernel.kallsyms]  [k] entry_SYSCALL_64_after_swapgs
   0.50%  penalty  penalty[.] runtime.lock
   0.46%  penalty  [kernel.kallsyms]  [k] __wake_up_bit
   0.43%  penalty  [kernel.kallsyms]  [k] smp_call_function_single
   0.39%  penalty  penalty[.] main.main.func1
   0.37%  penalty  [kernel.kallsyms]  [k] get_futex_key
   0.37%  penalty  penalty[.] runtime.notesleep
   0.35%  penalty  [kernel.kallsyms]  [k] do_futex
   0.32%  penalty  [kernel.kallsyms]  [k] hash_futex
   0.32%  penalty  [kernel.kallsyms]  [k] sys_futex
   0.32%  penalty  penalty[.] runtime.chanrecv
   0.32%  penalty  penalty[.] runtime.startm
   0.31%  penalty  penalty[.] runtime/internal/atomic.Load
   0.30%  penalty  penalty[.] runtime/internal/atomic.Cas
   0.30%  penalty  [kernel.kallsyms]  [k] do_select
   0.28%  penalty  [kernel.kallsyms]  [k] core_sys_select
   0.28%  penalty  [kernel.kallsyms]  [k] futex_wait_queue_me
   0.27%  penalty  penalty[.] runtime.chansend
   0.25%  penalty  penalty[.] runtime.casgstatus 


-- 
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] Re: Reflect Struct Comparison

2016-11-22 Thread iAdvice Edge
Hi Dave,

I need to create a common utility which checks whether the struct is empty
or not else i can throw error.
for example:

type Employee struct{
EmpId string
EmpName string
Department []String
Projects map[string]string
}

i need to check whether the object coming from rest api i hosted has empty
value or it is filled with something. and i want to make the utility not
limited to Employee but to use the same for other structs as well.

how can i do it?

Regards,
Parveen Kumar

On Tue, Nov 22, 2016 at 3:39 PM, Dave Cheney  wrote:

> Please do not reopen to a four year old thread. Instead please start a new
> thread describing the problem you have, what you tried, and what happened
> when you tried.
>
> Thanks
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/neGz_Rxtxxw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Cia


On Tuesday, 22 November 2016 18:08:37 UTC+8, Dave Cheney wrote:
>
> Related, you don't need a pointer to a chan, channels are already pointers 
> to the private runtime channel type.


Thanks for telling me that ☺ The codes will be pretty cleaner without the 
pointer '*' notation.

Is there something wrong with the scheduler? Or maybe a really deeply 
hidden bug in the codes. (Doubting about 1st one honestly)

-- 
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: Memory usage and go tool pprof

2016-11-22 Thread Alexander Petrovsky
Hello!

"Real" is what I see in atop/htop/ps, as i mentioned erlier.

Yep, about asked memory from OS is in my mind too, but how can I find 
situation when/why it's occur?

To be more precise, my app load N merabytes from DB, and put them into map. 
So, I fully understand when N megabytes transform to N gigabytes in memory 
in map, in the next iteration, after some time, my app doing load and 
transform again, so, I can imagine situation, when in exist two maps, N 
gigabytes each, but after that old map is gone, and my app start using new 
map! So, in my mind GC shoud kill old map, is I'm right? I How can I tune 
memory allocator? May be I can tune it to allocate memory more tight? Why 
my app don't return back allocated memory?

вторник, 22 ноября 2016 г., 0:10:20 UTC+3 пользователь Tamás Gulácsi 
написал:
>
> What is "real"?
> 65Gb has been asked from the OS, and it gave that much to the runtime. But 
> it used only 25Gb of it.
> Maybe even released some to the OS, but maybe that did not took that.
>
> Is the 25Gb really needed and used for good? The OOM suggests that for 
> moments your program allocated much more
>

-- 
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] Re: Reflect Struct Comparison

2016-11-22 Thread Dave Cheney
If you want to write a general function you will need to use the reflect 
package, specifically reflect.Value.IsValid to check each field to see if it 
contains the zero value or not.

-- 
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: Memory usage and go tool pprof

2016-11-22 Thread Dave Cheney
Please turn on gc debugging with end GODEBUG=gctrace=1 and check that your 
applications heap usage is behaving as you believe. 

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
Why do you want to use LockOSthread, you've proved it has a significant 
performance cost for your application.

-- 
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] Re: Reflect Struct Comparison

2016-11-22 Thread iAdvice Edge
i am new to Golang.

can you just share some piece of code just to check each field in struct
for empty or nil or zero value.

On Tue, Nov 22, 2016 at 4:19 PM, Dave Cheney  wrote:

> If you want to write a general function you will need to use the reflect
> package, specifically reflect.Value.IsValid to check each field to see if
> it contains the zero value or not.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/neGz_Rxtxxw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Cia


On Tuesday, 22 November 2016 18:51:46 UTC+8, Dave Cheney wrote:
>
> Why do you want to use LockOSthread, you've proved it has a significant 
> performance cost for your application.


 *Cgo* is the answer.

(Actually I want to use a C library(which needs LockOSthread to reach local 
constant thread execution model) through Cgo in the Go application.And one 
of the biggest benefits of Go is the Channel's event notification mechanism 
which is on the user space and powerful.It could be used to create a sync & 
nonblock application easily.)

Is this a expected performance penalty when we use channel with 
LockOSthread?(Maybe due to a design tradeoff or anything)

(: Just want to figure it out and be sure the exclusion of the probability 
of some bugs maybe hidden on this test codes here :)

-- 
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] Interface vs first-class function

2016-11-22 Thread roger peppe
>From my point of view, the main difference is that interfaces are strictly
more powerful, because you can't dynamically type convert a function
type into something different.

You can always make an interface from a function (e.g. http.HandlerFunc
and the like) but not the other way around.

This is a real issue in some places - for example http.Handler is an interface,
but the popular github.com/julienschmidt/httprouter package's Handle
type is a function, which means that you can't use the same router to
(for example) store metadata about routes, because all you've got is
a function.

Another consideration is that a function is only one word, so if you're
likely to be using stateless non-closure functions, and putting them
into interface values, you'll incur less allocations.

On 20 November 2016 at 05:22, Henry  wrote:
> Hi,
>
> I am wondering from best practice point of view whether it is better to use 
> interface or first-class function, especially considering that Go encourages 
> the use of small interfaces. Here is an example:
>
> type SomethingDoer interface{
>DoSomething(data Data)
> }
>
> func PerformWork(doer SomethingDoer){
>//...
> }
>
> Or
>
> type SomethingDoer func(Data)
>
> func PerformWork(doer SomethingDoer){
>//...
> }
>
> Is there some sort of a guideline when to use one vs the other? They seem 
> like redundant features to me.
>
> Thanks
>
> Henry
>
> --
> 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.

-- 
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] Re: Reflect Struct Comparison

2016-11-22 Thread Dave Cheney
This should get you started. https://play.golang.org/p/fKGkJPMToy

On Tuesday, 22 November 2016 21:58:31 UTC+11, iAdvice Edge wrote:
>
> i am new to Golang.
>
> can you just share some piece of code just to check each field in struct 
> for empty or nil or zero value.
>
> On Tue, Nov 22, 2016 at 4:19 PM, Dave Cheney  > wrote:
>
>> If you want to write a general function you will need to use the reflect 
>> package, specifically reflect.Value.IsValid to check each field to see if 
>> it contains the zero value or not.
>>
>> --
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "golang-nuts" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/golang-nuts/neGz_Rxtxxw/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> golang-nuts...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
The cgo call is going to cost you a lot more than this. Can you write up 
your real code and profile it.

On Tuesday, 22 November 2016 22:11:34 UTC+11, Cia wrote:
>
>
>
> On Tuesday, 22 November 2016 18:51:46 UTC+8, Dave Cheney wrote:
>>
>> Why do you want to use LockOSthread, you've proved it has a significant 
>> performance cost for your application.
>
>
>  *Cgo* is the answer.
>
> (Actually I want to use a C library(which needs LockOSthread to reach 
> local constant thread execution model) through Cgo in the Go 
> application.And one of the biggest benefits of Go is the Channel's event 
> notification mechanism which is on the user space and powerful.It could be 
> used to create a sync & nonblock application easily.)
>
> Is this a expected performance penalty when we use channel with 
> LockOSthread?(Maybe due to a design tradeoff or anything)
>
> (: Just want to figure it out and be sure the exclusion of the probability 
> of some bugs maybe hidden on this test codes here :)
>
>

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Cia
I have been tested Cgo's perf and here is the result below:

chan:
1000, op/s (buffer 100)
400, op/s (no buffer,blocked)

cgo:
554,4729 op/s

It shows that the speed of Cgo is acceptable,and even without thinking 
about the batch optimization method when using it *:)*

On Tuesday, 22 November 2016 19:30:55 UTC+8, Dave Cheney wrote:
>
> The cgo call is going to cost you a lot more than this. Can you write up 
> your real code and profile it.
>
> On Tuesday, 22 November 2016 22:11:34 UTC+11, Cia wrote:
>>
>>
>>
>> On Tuesday, 22 November 2016 18:51:46 UTC+8, Dave Cheney wrote:
>>>
>>> Why do you want to use LockOSthread, you've proved it has a significant 
>>> performance cost for your application.
>>
>>
>>  *Cgo* is the answer.
>>
>> (Actually I want to use a C library(which needs LockOSthread to reach 
>> local constant thread execution model) through Cgo in the Go 
>> application.And one of the biggest benefits of Go is the Channel's event 
>> notification mechanism which is on the user space and powerful.It could be 
>> used to create a sync & nonblock application easily.)
>>
>> Is this a expected performance penalty when we use channel with 
>> LockOSthread?(Maybe due to a design tradeoff or anything)
>>
>> (: Just want to figure it out and be sure the exclusion of the 
>> probability of some bugs maybe hidden on this test codes here :)
>>
>>

-- 
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] stupid question: how much processors/ram a twitter like web app (doing just CRUD operations) need to serve 1k request/s ?

2016-11-22 Thread Marwan abdel moneim
i know this depends on a lot of variables, but i don't need something 
accurate
if from your experience you could guess how much an app like this would 
need?

-- 
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] stupid question: how much processors/ram a twitter like web app (doing just CRUD operations) need to serve 1k request/s ?

2016-11-22 Thread Josh Kamau
To answer this kind of question, i normally write a simple app, then run it
on localhost, then  use ab tool to test.
e.g to send 100,000 requests 100 at a time,  ab -n 10 -c 100
http://localhost:3000/

Then i use 'top' to see how much cpu/ram its using.

On Tue, Nov 22, 2016 at 3:27 PM, Marwan abdel moneim 
wrote:

> i know this depends on a lot of variables, but i don't need something
> accurate
> if from your experience you could guess how much an app like this would
> need?
>
> --
> 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.
>

-- 
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] Client access to TLS Session-ID

2016-11-22 Thread laszlo . hordos
Hi, 

I'd like to implement a Client app in go which accesses to a Java HTTPS 
server and it uses the TLS Session-ID to protect against Man-in-the-middle 
attack and bind to one single TLS Session. When I use the Java Servlet the 
specification 
http://download.oracle.com/otn-pub/jcp/servlet-3.0-fr-eval-oth-JSpec/servlet-3_0-final-spec.pdf
 
expose the "javax.servlet.request.ssl_session_id" property and I have 
access to TLS Session-ID something like 
"032554E059DB27BF8CD87EBC53E9FF29376265F0BBFDBBFB7773D2277E5559F5"

Also when I use the $ openssl s_client -connect www.google.com:443 I get 
the  Session-ID too
SSL-Session:
Protocol  : TLSv1.2
Cipher: ECDHE-RSA-AES128-GCM-SHA256
Session-ID: 
38950231484EDAA2E55951FB86CBCED2A2A6074B6E1C5285B354626434397694

How can I access to the same information in Go?

I'm working with this code to access to Session-ID somehow but it looks 
like inaccessible 
from 
https://github.com/golang/go/blob/master/src/crypto/tls/handshake_client.go 

I'd appreciate any help or guidance about how can I fetch the Session-ID of 
a TLS connection.  

Thank you in advance. 

func main() {
   log.SetFlags(log.Lshortfile)

   tr := &http.Transport{
  DialTLS: dialTLSDefault,
  TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
   }
   httpClient := &http.Client{Transport: tr}

   res, err := httpClient.Get("https://www.google.com/robots.txt";)
   if err != nil {
  log.Fatal(err)
   }
   defer res.Body.Close()
   robots, err := ioutil.ReadAll(res.Body)
   if err != nil {
  log.Fatal(err)
   }

   fmt.Printf("%s", robots)

}

func dialTLSDefault(network, addr string) (net.Conn, error) {
   fmt.Printf("Connecto to: %s %s\n ", network, addr)
   cfg := &tls.Config{
  InsecureSkipVerify: true,
  ClientSessionCache: tls.NewLRUClientSessionCache(2),
  ServerName:"www.google.com",

   }
   cn, err := tls.Dial(network, addr, cfg)
   if err != nil {
  return nil, err
   }
   if err := cn.Handshake(); err != nil {
  return nil, err
   }
   if !cfg.InsecureSkipVerify {
  if err := cn.VerifyHostname(cfg.ServerName); err != nil {
 return nil, err
  }
   }
   state := cn.ConnectionState()

   if !state.NegotiatedProtocolIsMutual {
  return nil, errors.New("http: could not negotiate protocol 
mutually")
   }

   fmt.Printf("NegotiatedProtocol: %s\n ", state.NegotiatedProtocol)
   fmt.Printf("TLSUnique: %s\n ", hex.EncodeToString(state.TLSUnique))
   fmt.Printf("Session-ID: %s\n ", )

   return cn, 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.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] stupid question: how much processors/ram a twitter like web app (doing just CRUD operations) need to serve 1k request/s ?

2016-11-22 Thread Marwan abdel moneim
i will try that, thanks

On Tuesday, November 22, 2016 at 3:24:19 PM UTC+2, Josh Kamau wrote:
>
> To answer this kind of question, i normally write a simple app, then run 
> it on localhost, then  use ab tool to test. 
> e.g to send 100,000 requests 100 at a time,  ab -n 10 -c 100 
> http://localhost:3000/   
>
> Then i use 'top' to see how much cpu/ram its using.  
>
> On Tue, Nov 22, 2016 at 3:27 PM, Marwan abdel moneim  > wrote:
>
>> i know this depends on a lot of variables, but i don't need something 
>> accurate
>> if from your experience you could guess how much an app like this would 
>> need?
>>
>> -- 
>> 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...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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] Timezone parsing discrepancy

2016-11-22 Thread david . claridge
Thanks Shawn & Andrew for your replies.

Ignoring the TZ abbreviation when it doesn't happen to be the local TZ 
seems like somewhat undesirable behavior. Unfortunately I can't work around 
this by switching to numeric zones, since I'm not in control of the input 
I'm parsing. Is there a fundamental reason time.Parse() couldn't interpret 
all the TZ codes standardized in RFC-822? It seems time.LoadLocation(..) 
knows about them.  I'll make this suggestion on the issue you filed.

Regards,
David


On Monday, November 21, 2016 at 9:18:58 PM UTC-8, andrey mirtchovski wrote:
>
> > The last sentence of the time.Parse function is "To avoid such problems, 
> > prefer time layouts that use a numeric zone offset, or use 
> ParseInLocation." 
> > It specifically refers to using "named" time zones (such as PST) instead 
> of 
> > offsets, like -0700. 
>
> The whole paragraph there is important to read but it's terse and 
> needs elaboration. time.Parse works off the local timezone. If the 
> abbreviation specified in the data matches an abbreviation with a UTC 
> offset in the current local zone then that offset will be used. When 
> you run the documentation example in a different timezone the package 
> tries to lookup PST in the local zone file, it doesn't find it and 
> then creates a fake PST timezone with offset of zero. This means that 
> to have "PST Feb 3, 2013 at 7:54pm (PST)" work correctly on your 
> computer and to have time.Parse print "... -0800 PST" you must be in 
> the PST timezone or set your local timezone to it. 
>
> I created this little example to illustrate it: 
> https://play.golang.org/p/giQoGlik9m; below is a sample run on my 
> machine: 
>
> $ date 
> Mon 21 Nov 2016 21:59:23 MST 
> $ for i in CET PST PDT MST MDT GMT; do ./t $i; done 
> 2013-02-03 19:54:00 + CET 
> 2013-02-03 19:54:00 + PST 
> 2013-02-03 19:54:00 + PDT 
> 2013-02-03 19:54:00 -0700 MST 
> 2013-02-03 18:54:00 -0700 MST 
> 2013-02-03 19:54:00 + GMT 
>
> I've created an issue to gauge devs' opinion: 
> https://github.com/golang/go/issues/18012 
>
> The two workarounds there should work for you (numeric timezones and 
> ParseInLocation). 
>

-- 
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] Timezone parsing discrepancy

2016-11-22 Thread Ian Lance Taylor
On Tue, Nov 22, 2016 at 10:30 AM,   wrote:
>
> Ignoring the TZ abbreviation when it doesn't happen to be the local TZ seems
> like somewhat undesirable behavior. Unfortunately I can't work around this
> by switching to numeric zones, since I'm not in control of the input I'm
> parsing. Is there a fundamental reason time.Parse() couldn't interpret all
> the TZ codes standardized in RFC-822? It seems time.LoadLocation(..) knows
> about them.  I'll make this suggestion on the issue you filed.

I replied on the issue.

I note that if you know that the RFC 822 abbreviations are correct for
your use case, despite the fact that they are ambiguous
internationally, then you can use a simple string substitution to
replace those abbreviations with the equivalent numeric timezone.

Ian

-- 
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: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
Thinking about this overnight I think this benchmark

https://play.golang.org/p/5d4MUKqPYd

Shows the issue you are having. On my machine

% go test -bench=. -benchtime=5s 
BenchmarkWithoutLockOSThread-4 5000 1827196 ns/op 
BenchmarkWithLockOSThread-4 200 31506649 ns/op

My guess is the LockOSThread version causes a context switch because the 
thread the sending goroutine runs on must be parked, and the receiving 
thread locked to the receiving goroutine must be woken to drain the value. 

I don't see an easy workaround for this slowdown.

On Tuesday, 22 November 2016 22:43:34 UTC+11, Cia wrote:
>
> I have been tested Cgo's perf and here is the result below:
>
> chan:
> 1000, op/s (buffer 100)
> 400, op/s (no buffer,blocked)
>
> cgo:
> 554,4729 op/s
>
> It shows that the speed of Cgo is acceptable,and even without thinking 
> about the batch optimization method when using it *:)*
>
> On Tuesday, 22 November 2016 19:30:55 UTC+8, Dave Cheney wrote:
>>
>> The cgo call is going to cost you a lot more than this. Can you write up 
>> your real code and profile it.
>>
>> On Tuesday, 22 November 2016 22:11:34 UTC+11, Cia wrote:
>>>
>>>
>>>
>>> On Tuesday, 22 November 2016 18:51:46 UTC+8, Dave Cheney wrote:

 Why do you want to use LockOSthread, you've proved it has a significant 
 performance cost for your application.
>>>
>>>
>>>  *Cgo* is the answer.
>>>
>>> (Actually I want to use a C library(which needs LockOSthread to reach 
>>> local constant thread execution model) through Cgo in the Go 
>>> application.And one of the biggest benefits of Go is the Channel's event 
>>> notification mechanism which is on the user space and powerful.It could be 
>>> used to create a sync & nonblock application easily.)
>>>
>>> Is this a expected performance penalty when we use channel with 
>>> LockOSthread?(Maybe due to a design tradeoff or anything)
>>>
>>> (: Just want to figure it out and be sure the exclusion of the 
>>> probability of some bugs maybe hidden on this test codes here :)
>>>
>>>

-- 
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] Re: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Ian Lance Taylor
On Tue, Nov 22, 2016 at 12:37 PM, Dave Cheney  wrote:
> Thinking about this overnight I think this benchmark
>
> https://play.golang.org/p/5d4MUKqPYd
>
> Shows the issue you are having. On my machine
>
> % go test -bench=. -benchtime=5s
> BenchmarkWithoutLockOSThread-4 5000 1827196 ns/op
> BenchmarkWithLockOSThread-4 200 31506649 ns/op
>
> My guess is the LockOSThread version causes a context switch because the
> thread the sending goroutine runs on must be parked, and the receiving
> thread locked to the receiving goroutine must be woken to drain the value.
>
> I don't see an easy workaround for this slowdown.

I also have come to think it may be due to thread context switching.

Still, I also wrote up a benchmark, and I filed https://golang.org/issue/18023.

Ian

-- 
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] Receiving results from a database

2016-11-22 Thread vanmulders1992
I just started leaning Go and I've been stuck on this for a couple of days

Running on an App Engine dev server


The idea is to get an object I can work with with a format like this:


{
  "Results": [
{
  "username": "Test username",
  "username_sani": "testusername",
  "dob": "1992-09-10",
  "email": "",
  "gender": "M",
  "account_type": "user",
  "account_status": "enabled",
  "activated": 1,
  "country": "US",
  "weekly_playtime": "6",
  "game": [

  ],
  "primary_game": "",
  "activation_code": "BOxc9P2FomaaL4qKkQ2JMUIa1kgaYCJUlkfUqTs9",
  "password": "",
  "last_active": "2016-11-20 20:27:38",
  "lol": {
"verify_string": "ymn8Hm9ojfCzLqgZaZDi"
  },
  "login_history": [
{
  "date": "2016-11-20 20:03:46",
  "result": "login_ok",   
  "forward": "",
  "timezone": -300
}
  ],
  "current_login_att": 0,
  "reset_password_entries": [
{
  "code": "sZln9bCoLYVrPXErKes5",
  "valid_till": "2016-11-22 01:10",
  "result": "cancelled"
}
}
  ],
  "Includes": [],
  "IsStale": false,
  "IndexTimestamp": "2016-11-21T16:21:06.5157187Z",
  "TotalResults": 1,
  "SkippedResults": 0,
  "IndexName": "users_search",
  "IndexEtag": "0100--0069--0001AED9",
  "ResultEtag": "A85E1A89-3C87-F9AA-B0BA-41EC39CD225A",
  "Highlightings": {},
  "NonAuthoritativeInformation": false,
  "LastQueryTime": "2016-11-22T19:19:39.0784570Z",
  "DurationMilliseconds": 0,
  "ScoreExplanations": {},
  "TimingsInMilliseconds": {},
  "ResultSize": 0
}


My current code :


ctx := appengine.NewContext(req)
client := urlfetch.Client(ctx)
//
resp,_ := client.Get(ip+"/indexes/"+ query)
//
doc,_ := ioutil.ReadAll(resp.Body)
//
resp.Body.Close();
//
var index_results IndexResults
err := json.Unmarshal(doc,&index_results)
if err != nil {
log.Println(err)
}
log.Println(index_results) 


Struct
type IndexResults struct{
Results []byte `json:"Results"`
}

Which gives me an error : json: cannot unmarshal object into Go value of 
type uint8
With result {[0]}

How can I receive json results with dynamic fields that may or not be 
there? Do I define a struct for every possible result?

Thank you




-- 
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] Re: Why there is a so great performance penalty when the usage combines LockOSThread and channel?

2016-11-22 Thread Dave Cheney
Thanks Ian.

On Wed, 23 Nov 2016, 07:47 Ian Lance Taylor  wrote:

> On Tue, Nov 22, 2016 at 12:37 PM, Dave Cheney  wrote:
> > Thinking about this overnight I think this benchmark
> >
> > https://play.golang.org/p/5d4MUKqPYd
> >
> > Shows the issue you are having. On my machine
> >
> > % go test -bench=. -benchtime=5s
> > BenchmarkWithoutLockOSThread-4 5000 1827196 ns/op
> > BenchmarkWithLockOSThread-4 200 31506649 ns/op
> >
> > My guess is the LockOSThread version causes a context switch because the
> > thread the sending goroutine runs on must be parked, and the receiving
> > thread locked to the receiving goroutine must be woken to drain the
> value.
> >
> > I don't see an easy workaround for this slowdown.
>
> I also have come to think it may be due to thread context switching.
>
> Still, I also wrote up a benchmark, and I filed
> https://golang.org/issue/18023.
>
> Ian
>

-- 
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: Receiving results from a database

2016-11-22 Thread Tamás Gulácsi
First of all, please NEVER ignore the returned errors!
Then, print out "doc" to be sure.

In the concrete case, let Results be []json.RawMessage, or simply marshal 
into a map[string]interface{}.

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
Hi, 

How to architect the OO's virtual function in Go? 

Please take a look at this (not working) Go program
https://play.golang.org/p/qrBX6ScABp

Please think of the "func Output()" as a very complicated function that I 
only want to define *once *at the base level, not to duplicate into each 
sub classes. 

How can I make it works so that the last output statement, instead of 
being, 

fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())


will be this instead:

fmt.Printf("[%v] %s\n", k, v.Output())


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.


Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Jesse McNelis
On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun  wrote:
> Hi,
>
> How to architect the OO's virtual function in Go?
>
> Please take a look at this (not working) Go program
> https://play.golang.org/p/qrBX6ScABp
>
> Please think of the "func Output()" as a very complicated function that I
> only want to define once at the base level, not to duplicate into each sub
> classes.
>
> How can I make it works so that the last output statement, instead of being,
>
> fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>
>
> will be this instead:
>
> fmt.Printf("[%v] %s\n", k, v.Output())
>

You define a function:

func Output(s Shape) string {
   return s.Name() + "'s area size is " + s.Area()
}

Go uses interfaces for polymorphism.
Other OOP languages can use inheritance for polymorphism too, but Go
doesn't have inheritance.

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Seb Binet
On Tue, Nov 22, 2016 at 10:16 PM, Tong Sun  wrote:

> Hi,
>
> How to architect the OO's virtual function in Go?
>
> Please take a look at this (not working) Go program
> https://play.golang.org/p/qrBX6ScABp
>
> Please think of the "func Output()" as a very complicated function that I
> only want to define *once *at the base level, not to duplicate into each
> sub classes.
>
> How can I make it works so that the last output statement, instead of
> being,
>
> fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>
>
> will be this instead:
>
> fmt.Printf("[%v] %s\n", k, v.Output())
>
>
what about this:
 https://play.golang.org/p/DdZBiOgF2w

-s

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
On Tue, Nov 22, 2016 at 4:29 PM, Seb Binet wrote:

>
>
> On Tue, Nov 22, 2016 at 10:16 PM, Tong Sun wrote:
>
>> Hi,
>>
>> How to architect the OO's virtual function in Go?
>>
>> Please take a look at this (not working) Go program
>> https://play.golang.org/p/qrBX6ScABp
>>
>> Please think of the "func Output()" as a very complicated function that I
>> only want to define *once *at the base level, not to duplicate into each
>> sub classes.
>>
>> How can I make it works so that the last output statement, instead of
>> being,
>>
>> fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>>
>>
>> will be this instead:
>>
>> fmt.Printf("[%v] %s\n", k, v.Output())
>>
>>
> what about this:
>  https://play.golang.org/p/DdZBiOgF2w
>


Aha, that way. I've been banning my head for a while & you saved my day.
Thanks a lot!

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote:

> On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote:
> > Hi,
> >
> > How to architect the OO's virtual function in Go?
> >
> > Please take a look at this (not working) Go program
> > https://play.golang.org/p/qrBX6ScABp
> >
> > Please think of the "func Output()" as a very complicated function that I
> > only want to define once at the base level, not to duplicate into each
> sub
> > classes.
> >
> > How can I make it works so that the last output statement, instead of
> being,
> >
> > fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
> >
> >
> > will be this instead:
> >
> > fmt.Printf("[%v] %s\n", k, v.Output())
> >
>
> You define a function:
>
> func Output(s Shape) string {
>return s.Name() + "'s area size is " + s.Area()
> }
>
> Go uses interfaces for polymorphism.
> Other OOP languages can use inheritance for polymorphism too, but Go
> doesn't have inheritance.
>

Thanks Jesse. That works.

However I just realized that it is only part of my problem.

I have a huge list of common variables that I defined in my "base class",
changing it from a member function to a pure function causes almost every
single variable now undefined.

I can demonstrate the problem with this code
https://play.golang.org/p/QjCtD9rGpa

So, once again, thinking in OO, I'll define all of the common variables in
base class, and common functionalities in virtual functions. How to make
that idea work in Go?

For the above specific code, how to easily make "func Output" works?

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.


Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Jesse McNelis
On 23 Nov. 2016 9:03 am, "Tong Sun"  wrote:
>
> So, once again, thinking in OO, I'll define all of the common variables
in base class, and common functionalities in virtual functions. How to make
that idea work in Go?
>
> For the above specific code, how to easily make "func Output" works?
>

You can use functions and embedding for code reuse and interfaces for
polymorphism.

In your example you've implemented Speak() method for each type and defined
a Speaker interface but then in Output() you don't call Speak().

If you have fields you want available through the Speaker interface then
you need to define getter methods for those fields and add them to the
interface.

You could also add a Speak() method to the Animal type which would then be
available on any type that embeds an Animal and would have direct access to
any fields on Animal.

You could define a Speak() function that all the Speak() methods call for
some common functionality.

How you structure it depends on what real world problem you're trying to
solve.

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Nick Patavalis
Hi,

There is no direct mapping of what you can do with virtual functions in 
other OO languages, and Go. There are different compromises you have to 
make; because of this, synthetic examples will probably not help much. 

That being said, in the case of your last example I would make Output() a 
method of Animal. 
The Speak() methods of the specific animals would then call Output().

/npat

On Wednesday, November 23, 2016 at 12:03:54 AM UTC+2, Tong Sun wrote:
>
>
>
> On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote:
>
>> On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote:
>> > Hi,
>> >
>> > How to architect the OO's virtual function in Go?
>> >
>> > Please take a look at this (not working) Go program
>> > https://play.golang.org/p/qrBX6ScABp
>> >
>> > Please think of the "func Output()" as a very complicated function that 
>> I
>> > only want to define once at the base level, not to duplicate into each 
>> sub
>> > classes.
>> >
>> > How can I make it works so that the last output statement, instead of 
>> being,
>> >
>> > fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>> >
>> >
>> > will be this instead:
>> >
>> > fmt.Printf("[%v] %s\n", k, v.Output())
>> >
>>
>> You define a function:
>>
>> func Output(s Shape) string {
>>return s.Name() + "'s area size is " + s.Area()
>> }
>>
>> Go uses interfaces for polymorphism.
>> Other OOP languages can use inheritance for polymorphism too, but Go
>> doesn't have inheritance.
>>
>
> Thanks Jesse. That works. 
>
> However I just realized that it is only part of my problem. 
>
> I have a huge list of common variables that I defined in my "base class", 
> changing it from a member function to a pure function causes almost every 
> single variable now undefined. 
>
> I can demonstrate the problem with this code
> https://play.golang.org/p/QjCtD9rGpa
>
> So, once again, thinking in OO, I'll define all of the common variables in 
> base class, and common functionalities in virtual functions. How to make 
> that idea work in Go?
>
> For the above specific code, how to easily make "func Output" works?
>
> 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.


Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread paraiso . marc
interfaces only work on methods. 

https://play.golang.org/p/o6Ot4IdJZ1

Name, Age , ... are not methods they are fields.You need to make them part 
of Speaker interface by using methods instead of fields.

Le mardi 22 novembre 2016 23:03:54 UTC+1, Tong Sun a écrit :
>
>
>
> On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote:
>
>> On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote:
>> > Hi,
>> >
>> > How to architect the OO's virtual function in Go?
>> >
>> > Please take a look at this (not working) Go program
>> > https://play.golang.org/p/qrBX6ScABp
>> >
>> > Please think of the "func Output()" as a very complicated function that 
>> I
>> > only want to define once at the base level, not to duplicate into each 
>> sub
>> > classes.
>> >
>> > How can I make it works so that the last output statement, instead of 
>> being,
>> >
>> > fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>> >
>> >
>> > will be this instead:
>> >
>> > fmt.Printf("[%v] %s\n", k, v.Output())
>> >
>>
>> You define a function:
>>
>> func Output(s Shape) string {
>>return s.Name() + "'s area size is " + s.Area()
>> }
>>
>> Go uses interfaces for polymorphism.
>> Other OOP languages can use inheritance for polymorphism too, but Go
>> doesn't have inheritance.
>>
>
> Thanks Jesse. That works. 
>
> However I just realized that it is only part of my problem. 
>
> I have a huge list of common variables that I defined in my "base class", 
> changing it from a member function to a pure function causes almost every 
> single variable now undefined. 
>
> I can demonstrate the problem with this code
> https://play.golang.org/p/QjCtD9rGpa
>
> So, once again, thinking in OO, I'll define all of the common variables in 
> base class, and common functionalities in virtual functions. How to make 
> that idea work in Go?
>
> For the above specific code, how to easily make "func Output" works?
>
> 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.


Re: [go-nuts] Dynamic linking against individual golang standard libraries

2016-11-22 Thread Parker Evans
Alright, I will take a look.  Thanks for the response.

~Parker

On Monday, November 21, 2016 at 7:00:01 PM UTC-5, Ian Lance Taylor wrote:
>
> On Sun, Nov 20, 2016 at 1:22 PM, Parker Evans  > wrote: 
> > 
> > In order to do this kind of thing, do I need to manually figure out 
> > dependencies and then setup appropriate linker flags? 
>
> Yes, pretty much. 
>
> It may help a bit to look at go/build/deps_test.go. 
>
> Ian 
>

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
On Tue, Nov 22, 2016 at 6:23 PM, Jesse McNelis wrote:

> On 23 Nov. 2016 9:03 am, "Tong Sun" wrote:
> >
> > So, once again, thinking in OO, I'll define all of the common variables
> in base class, and common functionalities in virtual functions. How to make
> that idea work in Go?
> >
> > For the above specific code, how to easily make "func Output" works?
> >
>
> You can use functions and embedding for code reuse and interfaces for
> polymorphism.
>
> In your example you've implemented Speak() method for each type and
> defined a Speaker interface but then in Output() you don't call Speak().
>
Yeah, that Output() is the virtual function I'm implementing. In my real
code it has nothing to do with Speak() method, only to use those common
variables.

> If you have fields you want available through the Speaker interface then
> you need to define getter methods for those fields and add them to the
> interface.
>
Got it.

> You could also add a Speak() method to the Animal type which would then be
> available on any type that embeds an Animal and would have direct access to
> any fields on Animal.
>
However, different animals speak differently. I.e., it is not the fields on
Animal that matter but what outside it matter to Speak(), right? So adding
a Speak() method to the Animal doesn't make much sense, right?


> You could define a Speak() function that all the Speak() methods call for
> some common functionality.
>
> How you structure it depends on what real world problem you're trying to
> solve.
>
Probably, but just thinking in abstract terms, I thought there are some
basic principles that people can follow. Anyway, I think paraiso.marc's
methodology might be the only way.

>

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
No Nick, making Output() a member method won't work.
See my OP and Jesse's answer. I.e., I have to change it from a member
function to a pure function.

On Tue, Nov 22, 2016 at 6:25 PM, Nick Patavalis 
wrote:

> Hi,
>
> There is no direct mapping of what you can do with virtual functions in
> other OO languages, and Go. There are different compromises you have to
> make; because of this, synthetic examples will probably not help much.
>
> That being said, in the case of your last example I would make Output() a
> method of Animal.
> The Speak() methods of the specific animals would then call Output().
>
> /npat
>
> On Wednesday, November 23, 2016 at 12:03:54 AM UTC+2, Tong Sun wrote:
>>
>>
>>
>> On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote:
>>
>>> On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote:
>>> > Hi,
>>> >
>>> > How to architect the OO's virtual function in Go?
>>> >
>>> > Please take a look at this (not working) Go program
>>> > https://play.golang.org/p/qrBX6ScABp
>>> >
>>> > Please think of the "func Output()" as a very complicated function
>>> that I
>>> > only want to define once at the base level, not to duplicate into each
>>> sub
>>> > classes.
>>> >
>>> > How can I make it works so that the last output statement, instead of
>>> being,
>>> >
>>> > fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>>> >
>>> >
>>> > will be this instead:
>>> >
>>> > fmt.Printf("[%v] %s\n", k, v.Output())
>>> >
>>>
>>> You define a function:
>>>
>>> func Output(s Shape) string {
>>>return s.Name() + "'s area size is " + s.Area()
>>> }
>>>
>>> Go uses interfaces for polymorphism.
>>> Other OOP languages can use inheritance for polymorphism too, but Go
>>> doesn't have inheritance.
>>>
>>
>> Thanks Jesse. That works.
>>
>> However I just realized that it is only part of my problem.
>>
>> I have a huge list of common variables that I defined in my "base class",
>> changing it from a member function to a pure function causes almost every
>> single variable now undefined.
>>
>> I can demonstrate the problem with this code
>> https://play.golang.org/p/QjCtD9rGpa
>>
>> So, once again, thinking in OO, I'll define all of the common variables
>> in base class, and common functionalities in virtual functions. How to make
>> that idea work in Go?
>>
>> For the above specific code, how to easily make "func Output" works?
>>
>> Thanks
>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/f_62HEOIBV4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
Thanks a lot for your explicit example. Much more helpful to me than merely
saying define getters. Much appreciate it!

On Tue, Nov 22, 2016 at 6:27 PM,  wrote:

> interfaces only work on methods.
>
> https://play.golang.org/p/o6Ot4IdJZ1
>
> Name, Age , ... are not methods they are fields.You need to make them part
> of Speaker interface by using methods instead of fields.
>
>
> Le mardi 22 novembre 2016 23:03:54 UTC+1, Tong Sun a écrit :
>>
>>
>>
>> On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote:
>>
>>> On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote:
>>> > Hi,
>>> >
>>> > How to architect the OO's virtual function in Go?
>>> >
>>> > Please take a look at this (not working) Go program
>>> > https://play.golang.org/p/qrBX6ScABp
>>> >
>>> > Please think of the "func Output()" as a very complicated function
>>> that I
>>> > only want to define once at the base level, not to duplicate into each
>>> sub
>>> > classes.
>>> >
>>> > How can I make it works so that the last output statement, instead of
>>> being,
>>> >
>>> > fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
>>> >
>>> >
>>> > will be this instead:
>>> >
>>> > fmt.Printf("[%v] %s\n", k, v.Output())
>>> >
>>>
>>> You define a function:
>>>
>>> func Output(s Shape) string {
>>>return s.Name() + "'s area size is " + s.Area()
>>> }
>>>
>>> Go uses interfaces for polymorphism.
>>> Other OOP languages can use inheritance for polymorphism too, but Go
>>> doesn't have inheritance.
>>>
>>
>> Thanks Jesse. That works.
>>
>> However I just realized that it is only part of my problem.
>>
>> I have a huge list of common variables that I defined in my "base class",
>> changing it from a member function to a pure function causes almost every
>> single variable now undefined.
>>
>> I can demonstrate the problem with this code
>> https://play.golang.org/p/QjCtD9rGpa
>>
>> So, once again, thinking in OO, I'll define all of the common variables
>> in base class, and common functionalities in virtual functions. How to make
>> that idea work in Go?
>>
>> For the above specific code, how to easily make "func Output" works?
>>
>> Thanks
>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/f_62HEOIBV4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.