FAQ
I have the following library code:

===
package keeri
type keeri struct {
a int
}
func NewKeeri() *keeri {
return &keeri{}
}
===

Now I import the above go library in a go program as below:

===
package main

import "github.com/psankar/keeri/keeri"

func main() {
keeri.NewKeeri()
}
===

Here I am not capturing the return value of the function NewKeeri but I do
not get any compilation error. Should I be not getting a compilation error
unless I assign the return value of NewKeeri to _ through an assignment
operation ?

Why does it not give a compilation error ?

Sankar
http://psankar.blogspot.in


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Mar 12, 2015 at 9:29 am

    On Thu, Mar 12, 2015 at 10:15 AM Sankar wrote:

    Why does it not give a compilation error ?
    Because it is valid code: http://golang.org/ref/spec#Expression_statements

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sankar P at Mar 12, 2015 at 9:41 am
    2015-03-12 14:58 GMT+05:30 Jan Mercl <0xjnml@gmail.com>:
    On Thu, Mar 12, 2015 at 10:15 AM Sankar wrote:

    Why does it not give a compilation error ?
    Because it is valid code: http://golang.org/ref/spec#Expression_statements
    I feel really dumb now.

    After you explained, I looked at fmt.Println and that also seem to
    return values. So far I have just assumed that Println will never
    return anything as I have never got any compilation error for not
    handling its return values.

    Similarly if I have a "bufio.NewScanner(os.Stdout)" without capturing
    the return value, that also does not give an error.

    So, what are the times at which we should use the _ operator ? Only
    when we want to ignore one of multiple return values but want to
    capture others ? This is a revelation for me and I feel like such a
    newbie :)

    Thanks.
    -j

    --
    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/HVQG0p2wTog/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Sankar P
    http://psankar.blogspot.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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Charles Haynes at Mar 13, 2015 at 1:49 am
    I do it when I want to make it clear that I am actually discarding the
    return value, and am not just unaware of it.

    -- Charles
    On Thu, Mar 12, 2015 at 8:41 PM, Sankar P wrote:

    2015-03-12 14:58 GMT+05:30 Jan Mercl <0xjnml@gmail.com>:
    On Thu, Mar 12, 2015 at 10:15 AM Sankar wrote:

    Why does it not give a compilation error ?
    Because it is valid code:
    http://golang.org/ref/spec#Expression_statements

    I feel really dumb now.

    After you explained, I looked at fmt.Println and that also seem to
    return values. So far I have just assumed that Println will never
    return anything as I have never got any compilation error for not
    handling its return values.

    Similarly if I have a "bufio.NewScanner(os.Stdout)" without capturing
    the return value, that also does not give an error.

    So, what are the times at which we should use the _ operator ? Only
    when we want to ignore one of multiple return values but want to
    capture others ? This is a revelation for me and I feel like such a
    newbie :)

    Thanks.
    -j

    --
    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/HVQG0p2wTog/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Sankar P
    http://psankar.blogspot.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+unsubscribe@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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 12, '15 at 9:15a
activeMar 13, '15 at 1:49a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase