[go-nuts] Question regarding Golang Interfaces and composite struct

2022-04-28 Thread Glen D souza
Consider the following piece of code

type Dog struct {
}

type Walker interface {
Walks()
}

func (d *Dog) Walks() {

}

func CheckWalker(w Walker) {

}

func main() {
dog := Dog{}
CheckWalker(dog) -> cannot use dog (variable of type Dog) as Walker 
value in argument to CheckWalker: Dog does not implement Walker (method 
Walks has pointer receiver)compilerInvalidIfaceAssign 

}

When I run this code I get the error as show in the red above

Now consider this piece of code

type Dog struct {
}

func (d *Dog) Walks() {

}

type Bird struct {
}

func (b *Bird) Flys() {

}

type Flyer interface {
Flys()
}

type Walker interface {
Walks()
}

type Combined interface {
Walker
Flyer
}

type Animal struct {
Dog
Bird
}

func CheckCombined(c Combined) {

}

func Check(w Walker) {

}
func main() {
animal := &Animal{}
CheckCombined(animal)
}

This code runs without any error!
Since Animal struct is compose of Dog and Bird (not pointer to them), why 
this code is working where as only pointer to Dog and Bird implement the 
necessary methods to satisfy the interface Combined ?

-- 
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/80a3498b-b1e9-4a30-a55c-ef93a53e89e4n%40googlegroups.com.


Re: [go-nuts] Question regarding Golang Interfaces and composite struct

2022-04-28 Thread burak serdar
On Thu, Apr 28, 2022 at 9:49 AM Glen D souza  wrote:

> Consider the following piece of code
>
> type Dog struct {
> }
>
> type Walker interface {
> Walks()
> }
>
> func (d *Dog) Walks() {
>
> }
>
> func CheckWalker(w Walker) {
>
> }
>
> func main() {
> dog := Dog{}
> CheckWalker(dog) -> cannot use dog (variable of type Dog) as Walker
> value in argument to CheckWalker: Dog does not implement Walker (method
> Walks has pointer receiver)compilerInvalidIfaceAssign
> 
> }
>
> When I run this code I get the error as show in the red above
>
> Now consider this piece of code
>
> type Dog struct {
> }
>
> func (d *Dog) Walks() {
>
> }
>
> type Bird struct {
> }
>
> func (b *Bird) Flys() {
>
> }
>
> type Flyer interface {
> Flys()
> }
>
> type Walker interface {
> Walks()
> }
>
> type Combined interface {
> Walker
> Flyer
> }
>
> type Animal struct {
> Dog
> Bird
> }
>

Look at the Animal struct. The Walks and Flys methods are defined for
*Animal, not for Animal. The type Animal has no methods. Because of that,
*Animal implements the combined interface, and not Animal.

In your main(), you declare animal=&Animal{}, thus the variable `animal`
can be used where Combined is required.



>
> func CheckCombined(c Combined) {
>
> }
>
> func Check(w Walker) {
>
> }
> func main() {
> animal := &Animal{}
> CheckCombined(animal)
> }
>
> This code runs without any error!
> Since Animal struct is compose of Dog and Bird (not pointer to them), why
> this code is working where as only pointer to Dog and Bird implement the
> necessary methods to satisfy the interface Combined ?
>
> --
> 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/80a3498b-b1e9-4a30-a55c-ef93a53e89e4n%40googlegroups.com
> 
> .
>

-- 
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/CAMV2RqqWFrd-SZAVgh%2BbbTzY63ZEpmWU6%2B%2B0bkO-rWvhiaDpSQ%40mail.gmail.com.


[go-nuts] Re: Java to Go converter - 2

2022-04-28 Thread alex-coder
So, multithreading.
I found a simple example from Oracle with the wonderful name "Bad Threads" 
there:
https://docs.oracle.com/javase/tutorial/essential/concurrency/QandE/questions.html
 
, simplified it even more, called the example a new simple name One and 
extend a Converter to translate the code to Golang.
There is no similar processing for InterruptedException in go, so the 
Converter does not translate the processing of this interrupt in any way. 
If anyone has a relatively small example in Java, please share it.
*Java code:*
package run.me;
public class One {
  String msg;
  public static void main(  String[] args) throws InterruptedException {
One one=new One();
one.msg="Initial message";
System.out.println("main:before start:" + one.msg + " second part of");
new Second(one).start();
one.msg="after go start";
Thread.sleep(2000);
System.out.println("main:about to end:" + one.msg);
  }
}
class Second extends Thread {
  One one;
  public Second(  One one){
this.one=one;
  }
  public void run(){
try {
  sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("run:after sleep:" + one.msg);
one.msg="try to change msg";
  }
}
*Converter выдал:*
package main

import (
"fmt"
"os"
"time"
)

type One struct {
msg *string
}

func NewOne() *One {
var one One = One{}
return &one
}
func main() {
var args []string = os.Args
var o_dummy *One = NewOne()
o_dummy.One_main(&args)
}

/** generated method **/
func (one_one *One) One_main(args *[]string) {
var one *One = NewOne()
s := "Initial message"
one.msg = &s
fmt.Println("main:before start:" + *one.msg + " second part of")
go NewSecond(one).run()
s_s := "after go start"
one.msg = &s_s
time.Sleep(2000)
fmt.Println("main:about to end:" + *one.msg)
}

type Second struct {
one *One
}

func NewSecond(one *One) *Second {
var second Second = Second{one}
return &second
}
func (second *Second) run() {
time.Sleep(1000)
fmt.Println("run:after sleep:" + *second.one.msg)
s_s_s := "try to change msg"
second.one.msg = &s_s_s
}

The next example will probably be about channel's.
If anyone has an interesting example, write me, I'll think about how to 
solve it. I just can't promise that it will be quickly. :-)

Thank you to all !

четверг, 7 апреля 2022 г. в 18:52:25 UTC+3, alex-coder: 

> Thanks for the comments about generating code to handle exceptions. . Here 
> it is a new version.
> package main
>
> import (
> "fmt"
> "os"
> )
>
> type CatchException struct{}
>
> func main() {
>
> var args []string = os.Args
>
> var ce CatchException = CatchException{}
> ce.CatchException_main(args)
> }
>
> /** generated method **/
> func (catchException *CatchException) CatchException_main(args []string) {
> defer func() {
> if err := recover(); err != nil {
> exc := err.(Exception)
> switch exc.msg {
>
> case "Exception":
> fmt.Println("yes, I caught it")
> default:
> fmt.Println("No, something is not right")
> }
> }
> fmt.Println("finally processing")
> }()
>
> (&ThrowException{}).runme()
> }
>
> type ThrowException struct{}
>
> func (throwException *ThrowException) runme() {
> panic(Exception{"Exception"})
> }
>
> type Exception struct {
> msg string
> }
>
> понедельник, 4 апреля 2022 г. в 14:12:37 UTC+3, alex-coder: 
>
>>
>>
>>
>>
>> *Another use case for automatically translating codewritten in Java to 
>> Golang is Exception Handling.The next example is likely to be about 
>> multithreading.Example in Java:*
>> package com.builder.start.here;
>>
>> public class CatchException {
>>
>> public static void main(String[] args) {
>> try {
>> new ThrowException().runme();
>> } catch (Exception e) {
>> System.out.println("yes, I caught it");
>> } finally {
>> System.out.println("finally processing");
>> }
>>
>> }
>>
>> }
>>
>> class ThrowException{
>> public void runme() throws Exception{
>> throw new Exception();
>> }
>> }
>>
>> *Converter gave out:*
>>
>> package main
>>
>> import (
>> "fmt"
>> "os"
>> )
>>
>> type CatchException struct{}
>>
>>
>> func main() {
>>
>> var args []string = os.Args
>>
>> var ce CatchException = CatchException{}
>> ce.CatchException_main(args)
>> }
>>
>> /** generated method **/
>> func (catchException *CatchException) CatchException_main(args []string) {
>> defer func() {
>> if err := recover(); err != nil {
>> str := err.(string)
>> switch str {
>> case "Exception":
>> fmt.Println("yes, I caught it")
>> default:
>> fmt.Println("No, something is not right")
>> }
>> }
>> fmt.Println("finally processing")
>> }

[go-nuts] Re: Constant CPU usage on an idle HTTP server from Go 1.17

2022-04-28 Thread Klaus Post
This "smells" a lot like https://github.com/golang/go/issues/51654 - though 
the repro is with TLS.

This is very disruptive for us (MinIO) as well, seeing this at several 
customers. Obviously downgrading to Go 1.16.6 is less than optimal for us.

/Klaus

On Tuesday, 12 April 2022 at 14:55:41 UTC+2 Brian Candler wrote:

> Correct: "go run" builds a binary and then fork/execs the binary it has 
> built, to run it.
>
> On Tuesday, 12 April 2022 at 08:19:38 UTC+1 scorr...@gmail.com wrote:
>
>> By the way, the difference that I thought I saw between go run and the 
>> compiled version was because the pid of the process to look at in the below 
>> example should be 63668, the tmp file, not 63548, the go run call.
>>
>> $ ps aux | grep test
>> bill   63548  0.2  0.1 2044984 17588 pts/0   Sl+  09:15   0:00 go run 
>> test.go
>> bill   63668  155  0.0 1891292 12140 pts/0   Sl+  09:15   1:52 
>> /tmp/go-build910547164/b001/exe/test
>> bill   63750  0.0  0.0  11564   652 pts/1S+   09:16   0:00 grep 
>> --color=auto test
>>
>> El martes, 12 de abril de 2022 a las 9:13:41 UTC+2, Santiago Corredoira 
>> escribió:
>>
>>> Hi, yes, all about the toy program is the same. It was an attempt to get 
>>> to a minimal example that anyone can reproduce so I could ask for help. The 
>>> real problem is with a larger program where I get this 100% CPU usages 
>>> ramdomly when the server is idle.
>>>
>>> I was thinking and with the basic example the nanosleep calls maybe is 
>>> not the scheduller signaling itself because:
>>>
>>>  - GODEBUG=asyncpreemptoff=1 has no effect
>>>  - It doesn't happen with Go 1.16
>>>  - It is random. If it where the normal working of the scheduler, 
>>> wouldn't it be predictable?
>>>
>>>
>>> El martes, 12 de abril de 2022 a las 8:59:06 UTC+2, Brian Candler 
>>> escribió:
>>>
 At the start of the thread, you said "if I compile and run this simple 
 program ..."

 Does the problem no longer reproduce with that?

 On Monday, 11 April 2022 at 22:59:55 UTC+1 scorr...@gmail.com wrote:

> I tried with a more complex application and already got constant 100% 
> CPU usage twice. 
>
> When I attach strace I see the same as with the minimal demo but with 
> much more frecuency:
>
> [pid 42659] 23:36:49.035120 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035133 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035145 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035158 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035170 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035182 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035195 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035207 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035219 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035232 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035244 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035257 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035269 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035281 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
> [pid 42659] 23:36:49.035293 epoll_pwait(3, [], 128, 0, NULL, 
> 140341803388040) = 0
> [pid 42659] 23:36:49.035306 epoll_pwait(3, [], 128, 0, NULL, 
> 18704162493558) = 0
>
> The program is too large to post here. I have tested it with go race 
> and it doesn't run into any problem. Any idea of what to look for?
>
> Thank you!
>
> El lunes, 11 de abril de 2022 a las 17:08:40 UTC+2, Santiago 
> Corredoira escribió:
>
>> I tried with the stripped tmp version of "go run" and after a while I 
>> was able to reproduce it too. It definitely took me more time but 
>> probably 
>> is just random. 
>>
>> I tried "$ GODEBUG=asyncpreemptoff=1 ./test" and I kept seeing the 
>> the calls every 10ms. Is this expected to happen?
>>
>> I compiled the test with "$ go1.16 build test.go" and I tried hard to 
>> reproduce it but I couldn't. It is very strange because this behaviour 
>> was 
>> introduced in Go 1.14 and I only see it in Go 1.17. Also 
>> asyncpreemptoff=1 
>> seems to make no difference.
>>
>> I am going to work again with Go 1.18 by default to see if I get the 
>> 100% CPU usage.
>>
>> El lunes, 11 de abril de 2022 a las 13:28:48 UTC+2, Brian Candler 
>> escribió:
>>
>>> On Monday, 11 April 2022 at 09:26:28 UTC+1 scorr...@gmail.com wr

[go-nuts] Taming SQL and ORMs with sqlc — go get it #001

2022-04-28 Thread ean...@gmail.com
We’ve just started a new article series about excellent Go packages and 
tools that you might not have heard about called "go get it". First out is 
an article about sqlc, an awesome tool for managing database queries. Check 
it out here . Would love your 
feedback!

Thanks,
André



-- 
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/a73c9fd9-cb6a-4623-a3c3-f0218c3c8b73n%40googlegroups.com.


[go-nuts] simple line plot in realtime

2022-04-28 Thread Daniel Jankins
Hi,

I have been using github.com/tadvi/winc and it works well.  I would like a
real time plot of data.  It would be a simple line plot that got update
every second or so.

Can anyone point me to some examples?

Thank you
-- 
DanJ

-- 
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/CACHdq71qAjH6ZQ%2BgESM%3DoED2FmAg%3DpbZZ6%3Dd2dxHLkjCZdG-qg%40mail.gmail.com.