[go-nuts] Re: x/net/Listen behaviours

2019-09-06 Thread Jacques Supcik
Yes! I think that your analysis is correct. The listener inside the if is 
not the same as the one outside. You can check using fmt.Println() and 
you will see two different addresses.

-- Jacques



Le vendredi 6 septembre 2019 15:52:11 UTC+2, jgr...@ou.edu a écrit :
>
> Ahh, I think I understand the problem!
>
> I assumed that "listener, err := ..." would use the existing listener 
> variable 
> (creating err on the spot), rather than creating a new locally-scoped 
> listener?
>
> Therefore, as you said, listener is basically always nil outside that "if 
> useListener>0 {}" scope? So server.ListenAndServe() is always invoked?
>
> If so, then - thanks a lot for the help! :)
>
> Cheers,
>
> J.
>
> On Friday, September 6, 2019 at 8:09:37 AM UTC-5, Jacques Supcik wrote:
>>
>> Hello,
>>
>> I tried your program and when you call listener, err := 
>> net.Listen("tcp4", addrString) it does indeed only listen to "tcp4". 
>> Later, when you call server.ListenAndServe() (because listener is nil), 
>> the system seems to "re-listen" from the same port and then it listen to 
>> "tcp" and not only "tcp4" (
>> https://github.com/golang/go/blob/master/src/net/http/server.go#L2826)
>>
>> I don't understand why you call both "net.Listen" and 
>> "server.ListenAndServe". If you want an HTTP server, I think that you can 
>> just use "server.ListenAndServe" and you don't need anything more. If you 
>> want an HTTP server and a generic TCP server, then you need 2 different 
>> ports, one for the HTTP server and another for the generic TCP server. But 
>> perhaps I did not understand your problem ;-)
>>
>> -- Jacques
>>
>>
>> Le jeudi 5 septembre 2019 22:13:31 UTC+2, jgr...@ou.edu a écrit :
>>>
>>> Hi!
>>>
>>> I'm having some confusion over the behaviour of net.Listen() and it's 
>>> interactions with http.Server.
>>>
>>> Can anyone take a look at this, and let me know what I'm doing wrong?
>>>
>>> Thanks!
>>>
>>>
>>> System description
>>> -
>>>
>>> Go: go version go1.12.9 darwin/amd64
>>>
>>> OS: macOS Mojave (10.14.6)
>>>
>>>
>>> Problem description
>>> --
>>>
>>> Passing a net.Listener from net.Listen() into the Serve() method of an 
>>> http.Server does not behave how I expect ...
>>>
>>>
>>> Test program
>>> -
>>>
>>> A simple server that responds to connections by echoing info regarding 
>>> the URL by which it was contacted (see below):
>>>
>>> package main
>>>
>>> import (
>>> "context"
>>> "flag"
>>> "fmt"
>>> "log"
>>> "net"
>>> "net/http"
>>> "os"
>>> "os/signal"
>>> "strconv"
>>> "syscall"
>>> "time"
>>> )
>>>
>>> // Print information about the local machine's network interfaces
>>> func printNetworkInterfaces() {
>>> ifaces, err := net.Interfaces()
>>> if err != nil { panic("net.Interfaces()") }
>>>
>>> if len(ifaces)<1 {
>>> log.Println("No network interfaces found.")
>>> return
>>> }
>>>
>>> hostname, _ := os.Hostname()
>>> log.Println( "Network interfaces for " + hostname )
>>>
>>> for _, iface := range ifaces {
>>> addrs, err := iface.Addrs()
>>> if err != nil { panic("iface.Addrs()") }
>>> if len(addrs) < 1 { continue }
>>> log.Println("-",iface.Name,iface.HardwareAddr)
>>> for _, addr := range addrs {
>>> switch v := addr.(type) {
>>> case *net.IPNet:
>>> str := fmt.Sprintf("IPNet: IP=%s, mask=%s, network=%s, string=%s", v.IP, 
>>> v.Mask, v.Network(), v.String())
>>> log.Println(" ", str)
>>> case *net.IPAddr:
>>> str := fmt.Sprintf("IPAddr: IP=%s, zone=%s, network=%s, string=%s", 
>>> v.IP, v.Zone, v.Network(), v.String())
>>> log.Println(" ", str)
>>> default:
>>> log.Println("")
>>> }
>>> }
>>> }
>>> }
>>>
>>> // Just write the incoming url back to the sender
>>> func echoHandler(w http.ResponseWri

[go-nuts] Re: x/net/Listen behaviours

2019-09-06 Thread Jacques Supcik
Hello,

I tried your program and when you call listener, err := net.Listen("tcp4", 
addrString) it does indeed only listen to "tcp4". Later, when you call 
server.ListenAndServe() (because listener is nil), the system seems to 
"re-listen" from the same port and then it listen to "tcp" and not only 
"tcp4" (
https://github.com/golang/go/blob/master/src/net/http/server.go#L2826)

I don't understand why you call both "net.Listen" and 
"server.ListenAndServe". If you want an HTTP server, I think that you can 
just use "server.ListenAndServe" and you don't need anything more. If you 
want an HTTP server and a generic TCP server, then you need 2 different 
ports, one for the HTTP server and another for the generic TCP server. But 
perhaps I did not understand your problem ;-)

-- Jacques


Le jeudi 5 septembre 2019 22:13:31 UTC+2, jgr...@ou.edu a écrit :
>
> Hi!
>
> I'm having some confusion over the behaviour of net.Listen() and it's 
> interactions with http.Server.
>
> Can anyone take a look at this, and let me know what I'm doing wrong?
>
> Thanks!
>
>
> System description
> -
>
> Go: go version go1.12.9 darwin/amd64
>
> OS: macOS Mojave (10.14.6)
>
>
> Problem description
> --
>
> Passing a net.Listener from net.Listen() into the Serve() method of an 
> http.Server does not behave how I expect ...
>
>
> Test program
> -
>
> A simple server that responds to connections by echoing info regarding the 
> URL by which it was contacted (see below):
>
> package main
>
> import (
> "context"
> "flag"
> "fmt"
> "log"
> "net"
> "net/http"
> "os"
> "os/signal"
> "strconv"
> "syscall"
> "time"
> )
>
> // Print information about the local machine's network interfaces
> func printNetworkInterfaces() {
> ifaces, err := net.Interfaces()
> if err != nil { panic("net.Interfaces()") }
>
> if len(ifaces)<1 {
> log.Println("No network interfaces found.")
> return
> }
>
> hostname, _ := os.Hostname()
> log.Println( "Network interfaces for " + hostname )
>
> for _, iface := range ifaces {
> addrs, err := iface.Addrs()
> if err != nil { panic("iface.Addrs()") }
> if len(addrs) < 1 { continue }
> log.Println("-",iface.Name,iface.HardwareAddr)
> for _, addr := range addrs {
> switch v := addr.(type) {
> case *net.IPNet:
> str := fmt.Sprintf("IPNet: IP=%s, mask=%s, network=%s, string=%s", v.IP, 
> v.Mask, v.Network(), v.String())
> log.Println(" ", str)
> case *net.IPAddr:
> str := fmt.Sprintf("IPAddr: IP=%s, zone=%s, network=%s, string=%s", v.IP, 
> v.Zone, v.Network(), v.String())
> log.Println(" ", str)
> default:
> log.Println("")
> }
> }
> }
> }
>
> // Just write the incoming url back to the sender
> func echoHandler(w http.ResponseWriter, r *http.Request) {
> txt := fmt.Sprintf("Echo: (%s)",r.URL.Path)
> w.Write( []byte(txt+"\n") )
> log.Println(txt)
> }
>
> var (
> listener_ = flag.Int("listener", 0, "Use an explicit net.Listener.")
> port_ = flag.Int("port", 0, "Set the port to listen on (0 = any free 
> port?).")
> timeout_  = flag.Int("wait", 0, "Timout (in seconds) before server killed 
> (0 = no timout).")
> )
>
> func main() {
>
> onShutdown := func(what string, cleanup func()) {
> log.Println( fmt.Sprintf("- Shutting down %s ...",what) )
> cleanup()
> log.Println( fmt.Sprintf("  %s shut down.",what) )
> }
>
> flag.Parse()
>
> useListener := *listener_
> port := *port_
> timeout := *timeout_
>
> // Let's see what interfaces are present on the local machine
>
> printNetworkInterfaces()
>
> // Simple server for incoming connections.
>
> http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) 
> {echoHandler(w,r)});
>
> var listener net.Listener = nil
> addrString := fmt.Sprintf(":%d",port)
>
> // Using an explicit Listener provides more control over the specifics,
> // e.g. tcp4/6 and letting the system select a currently free port.
>
> if useListener>0 {
> log.Println("Using net.Listener")
>
> listener, err := net.Listen("tcp4", addrString) // :0 -> use any free port
> if err != nil { log.Fatalln(err) }
>
> defer onShutdown("listener", func() {listener.Close()} )
>
> addrString = listener.Addr().String()
> host, portStr, err := net.SplitHostPort(addrString) // as port may have 
> been assigned by system
> if err != nil { log.Fatalln(err) }
>
> log.Println( fmt.Sprintf("Listener Addr string: %s (host: %s, port: 
> %s)",addrString,host,portStr) )
>
> port, err = strconv.Atoi(portStr)
> if err != nil { log.Fatalln(err) }
>
> addrString = fmt.Sprintf(":%d",port) // as port may have been assigned by 
> the system
> }
>
> server := http.Server { Addr: addrString }
>
> // Run web server in a separate goroutine so it doesn't block our progress
>
> go func(server *http.Server, listener net.Listener) {
>
> var err error
>
> if listener == nil {
> err = server.ListenAndServe()
> } else {
> err = server.Serve(listener)
> }
>
> switch err {
> case nil:
> case http.ErrServerClosed:
> log.Println("Caught ErrServerClosed")
> default:
> panic(err)
> }
> }(, 

Re: [go-nuts] Re: how to merge different struct to one struct

2019-08-24 Thread Jacques Supcik
I'm glad I could help.

Cheers,

-- Jacques

-- 
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/CAMkq311CMU%3D06A6gPFv2fvZwDPAK9MRxjnMXKk2swsQ2--DATw%40mail.gmail.com.


[go-nuts] Re: how to merge different struct to one struct

2019-08-24 Thread Jacques Supcik
As suggested by Kurtis Rader, I would use "reflection" for this.

Here is a simple example (not fully tested, probably not safe, not 
production ready... so just for demonstration purpose) :

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

-- Jacques

-- 
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/b3a3d328-22a4-4698-9cac-99a8e7960fec%40googlegroups.com.


[go-nuts] Re: Running tests per package with Go module

2019-08-20 Thread Jacques Supcik
Hello Chris,

I made a small project with the same structure as yours to reproduce the 
problem : https://github.com/supcik/rtppwgm

But in my project, everything works as expected :

» go test -count=1 ./...
ok  demo/pkg1   0.006s
ok  demo/pkg2   0.006s

» go test -count=1 ./pkg1/...
ok  demo/pkg1   0.006s

» go test -count=1 ./pkg2/...
ok  demo/pkg2   0.005s

Can you make a similar project that exposes your issue ?

Cheers,

-- Jacques

-- 
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/22e3a0fc-ed57-4b53-9ad5-faefab58c800%40googlegroups.com.