[go-nuts] No time.Duration#UnmarshalText; is there a good reason?

2022-01-17 Thread Corin Lawson
It seems obvious (to me) that the encoding.TextUnmarshaler interface could 
be implemented for time.Duration (it is implemented for time.Time, 
afterall).

The fact that it is not gives me pause... is there a good reason that the 
stdlib has not done this?  What issues am I facing if I do this: 
https://go.dev/play/p/nHBfS7TJQtJ

```
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (d *Duration) UnmarshalText(data []byte) error {
val, err := time.ParseDuration(string(data))
*d = Duration(val)
return err
}
```

Cheers,
Corin.

-- 
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/4f4029b8-cf01-4a71-837f-2002b398158en%40googlegroups.com.


Re: [go-nuts] Not used var error

2022-01-17 Thread Ian Lance Taylor
On Mon, Jan 17, 2022 at 7:07 PM Paulo Júnior  wrote:
>
> I'm studying about if statement and comma ok construction and I'm not sure 
> why the piece of code (https://go.dev/play/p/ScFJsih6lwR) bellow does not 
> work. The compiler says that: x declared but not used. However, x is used in 
> the second if statement of the main function, as you can see.
>
> It is worthy to notice that  x is just assigned (not declared) in the first 
> if of the main function, because I declared it as follows: var x int
>
> func MyFunc() (int, bool) {
> return 1, true
> }
>
> func main() {
> var x int
> if x, ok := MyFunc(); !ok {
> fmt.Println("Err")
> }
> fmt.Println(x)
> }

When you write "if x, ok := ... { }" that declares a new variable "x"
(and a new variable "ok") that are only visible in the if statement.
The "x" declared in the if statement shadows the "x" declared in "var
x int".  That is, there are two different variables named "x" here.
The one declared in the "if" statement is not used.

It looks like you may want to write something like

var x int
var ok bool
if x, ok = MyFunc(); !ok {
fmt.Println("Err")
}
fmt.Println(x)

Note the use of "=" rather than ":=" in the if statement.

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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcX8-%3DHA0FjytBVee4kPS2--%3DYFSctLo5407CJTURLrO_A%40mail.gmail.com.


[go-nuts] Not used var error

2022-01-17 Thread Paulo Júnior
Hi all.

I'm studying about *if statement* and *comma ok construction* and I'm not 
sure why the piece of code (https://go.dev/play/p/ScFJsih6lwR) bellow does 
not work. The compiler says that: *x declared but not used*. However, *x* is 
used in the second *if statement *of the *main function*, as you can see.

It is worthy to notice that  *x* is just assigned (not declared) in the 
first *if *of the *main function*, because I declared it as follows: *var x 
int*  

func MyFunc() (int, bool) {
return 1, true
}

func main() {
var x int
if x, ok := MyFunc(); !ok {
fmt.Println("Err")
}
fmt.Println(x)
}

Thank you and best regards, 
Paulo.

-- 
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/5804dce1-afa5-4045-96b5-cf9418da1c92n%40googlegroups.com.


Re: [go-nuts] Add to existing Go Package

2022-01-17 Thread Richard Masci
OK Thanks!!

On Mon, Jan 17, 2022 at 1:03 PM Axel Wagner 
wrote:

> On Mon, Jan 17, 2022 at 6:57 PM Rich  wrote:
>
>> What I am worried about is that my version of log will diverge from the
>> Go standard log package.   Is there a way to extend an existing package so
>> that my adds to the log package pulls from whatever happens to be the most
>> current version of log?
>>
>
> No. You can only either a) build something on top of it (i.e. a wrapper),
> or b) copy the package with your own modifications and distribute that.
>
>
>> --
>> 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/60ae5b79-b1b9-451d-b8bb-d3726f22ab71n%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/CAJ0CmuFutv6nusSSaybw8DfSHpiiM-WU%2BdFDSvfXU94uVrvc1w%40mail.gmail.com.


[go-nuts] Re: Add to existing Go Package

2022-01-17 Thread Uli Kunitz
Use log.SetOutput and the io.MultiWriter function to replace the output 
writer of the log package to write to stdout and a file at the same time.

log.SetOutput(io.MultiWriter(os.Stdout, f))

There is a limited capability to influence the timestamp with the SetFlags 
function. You could also replace the Writer with a Writer that modifies the 
time stamp at the beginning of every line. But if you do that, it is 
probably better to copy the  log package and create the timestamp you want. 
You would need to change the formatHeader method of the Logger type.

You could also check the log packages that are out there, whether they fit 
you. You may want to use logrus, which provides some compatibility to the 
stdlib log package and it allows time formst strings in its TextFormatter.
On Monday, January 17, 2022 at 6:56:44 PM UTC+1 Rich wrote:

> What I want to do is extend the go standard log package. I am using log to 
> write logfiles, but there are two things with it I need to extend. 
>
> The first is a 'Format' string, which allows me to change the time format 
> string log uses.
>
> The second is to be able to print to both logfile and stdout., This is 
> useful when you're starting your server, and you want some of the output to 
> go directly to stdout. I use this so that with one line I can print the 
> same line to stdout as I print to my log file.
>
> lgOut.Tprintf("Server Version: %s Started and listens on:  %s:%s, ", 
> server.Version, server.Address, server.Port)
>
> That would print to both stdout and to the logfile lgOut is configured for.
> Ex: 
> 20220117-13:45 | Server Version 1.0.1 Started and listens on 
> 127.0.0.1:8080"
>  
> What I am worried about is that my version of log will diverge from the Go 
> standard log package.   Is there a way to extend an existing package so 
> that my adds to the log package pulls from whatever happens to be the most 
> current version of log?
>

-- 
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/a5e3e140-bc5f-41dc-8971-7adf28d5c0cfn%40googlegroups.com.


Re: [go-nuts] Add to existing Go Package

2022-01-17 Thread 'Axel Wagner' via golang-nuts
On Mon, Jan 17, 2022 at 6:57 PM Rich  wrote:

> What I am worried about is that my version of log will diverge from the Go
> standard log package.   Is there a way to extend an existing package so
> that my adds to the log package pulls from whatever happens to be the most
> current version of log?
>

No. You can only either a) build something on top of it (i.e. a wrapper),
or b) copy the package with your own modifications and distribute that.


> --
> 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/60ae5b79-b1b9-451d-b8bb-d3726f22ab71n%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/CAEkBMfHR_g1u4JyyYj2153YdF_m7E0oP71Biha623941yMadAA%40mail.gmail.com.


[go-nuts] Add to existing Go Package

2022-01-17 Thread Rich
What I want to do is extend the go standard log package. I am using log to 
write logfiles, but there are two things with it I need to extend. 

The first is a 'Format' string, which allows me to change the time format 
string log uses.

The second is to be able to print to both logfile and stdout., This is 
useful when you're starting your server, and you want some of the output to 
go directly to stdout. I use this so that with one line I can print the 
same line to stdout as I print to my log file.

lgOut.Tprintf("Server Version: %s Started and listens on:  %s:%s, ", 
server.Version, server.Address, server.Port)

That would print to both stdout and to the logfile lgOut is configured for.
Ex: 
20220117-13:45 | Server Version 1.0.1 Started and listens on 127.0.0.1:8080"
 
What I am worried about is that my version of log will diverge from the Go 
standard log package.   Is there a way to extend an existing package so 
that my adds to the log package pulls from whatever happens to be the most 
current version of log?

-- 
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/60ae5b79-b1b9-451d-b8bb-d3726f22ab71n%40googlegroups.com.