duration^2

88 views
Skip to first unread message

andreas graeper

unread,
Jul 19, 2022, 12:14:19 PM7/19/22
to golang-nuts
hello,
i am new to everything is new in programming-heaven. old-c-guy. and i thought it`s time to learn something most-modern ..

time.NewTicker( time.Second )           ok
time.NewTicker( 5 * time.Second )     ok
var n int = 5 
time.NewTicker( n * time.Second ) error : int * duration
time.NewTicker( time.Duration(n) * time.Second )      ok : duration * duration [s^2] square-seconds ?

tick := time.NewTicker( time.Duration( math.rand.Intn(10) ) * time.Second )

go func() { for { select {
 case <-done : return
,case tc := <-tick.C : **** tick ****
}}}()

this strategy gets the tick if it has already happend in past and this go-routine is active
if it is suspended will it be notified as soon in either done or tick something is to read ?

when i (later) write own code / interfaces / struct where inside is like in ticker the .C a channel, can i define something like an operator, so that the outer object acts like a channel

type S struct {
 C chan time
 ..
}
s := S{C:make(chan time),..}
s<-
<-s

Amnon

unread,
Jul 19, 2022, 12:24:44 PM7/19/22
to golang-nuts
On Tuesday, 19 July 2022 at 17:14:19 UTC+1 agra...@googlemail.com wrote:
hello,
i am new to everything is new in programming-heaven. old-c-guy. and i thought it`s time to learn something most-modern ..

Welcome!

Go is a great language for old-c-guys (and girls). Go was,  in fact mainly invented by them...
You will have fun here...


 


time.NewTicker( time.Second )           ok
time.NewTicker( 5 * time.Second )     ok
var n int = 5 
time.NewTicker( n * time.Second ) error : int * duration
time.NewTicker( time.Duration(n) * time.Second )      ok : duration * duration [s^2] square-seconds ?

Yes, this bothers the inner Physicist in me too.
But you can only multiply numbers if they are of the same type...
 


tick := time.NewTicker( time.Duration( math.rand.Intn(10) ) * time.Second )

go func() { for { select {
 case <-done : return
,case tc := <-tick.C : **** tick ****
}}}()

this strategy gets the tick if it has already happend in past and this go-routine is active
if it is suspended will it be notified as soon in either done or tick something is to read ?

Yes
 


when i (later) write own code / interfaces / struct where inside is like in ticker the .C a channel, can i define something like an operator, so that the outer object acts like a channel

You want user defined operations - so you can define the implementation of the <- operator on your type.
 But we don't have this in Go.
You need to use methods. (Less syntactic sugar, but more clarity).


Dan Kortschak

unread,
Jul 19, 2022, 5:57:10 PM7/19/22
to golan...@googlegroups.com
On Tue, 2022-07-19 at 09:24 -0700, Amnon wrote:
> > time.NewTicker( time.Duration(n) * time.Second )      ok : duration
> > * duration [s^2] square-seconds ?
>
> Yes, this bothers the inner Physicist in me too.
> But you can only multiply numbers if they are of the same type...

With a small amount of gymnastics a reasonable comfort can be found;
types aren't units, but can be used to decorate them as such. This
means they underly the notion of a unit. (Tangentially, there are
packages that do dimensional operations, for example
https://pkg.go.dev/gonum.org/v1/gonum/unit).

Reply all
Reply to author
Forward
0 new messages