Search code examples
pointersgosyslog

Implement a function which returns a pointer


I have implemented the syslog daemon service in my golang app. I used syslog.New in main package and it works but now, I want to export it to another package.

package config

import (
    "log/syslog"
)

func LogBook() ? {
    sysLog, _ := syslog.New(syslog.LOG_LOCAL0|syslog.LOG_ERROR, "myapp") // syslog.New returns (*Writer, error)
    return ?
}

How can I implement this function? After, how can I use this variable 'sysLog' in other packages?

Thank you!


Solution

  • The answer is pretty simple as @Volker said,

    func LogBook() *syslog.Writer {
        sysLog, _ := syslog.New(syslog.LOG_LOCAL0|syslog.LOG_ERROR, "myapp")
        return sysLog
    }
    

    Usage Example:

    func main(){
        w := LogBook()
        w.Info("message")
    }
    

    Please notice:

    • This package is not implemented on Windows. As the syslog package is frozen, Windows users are encouraged to use a package outside of the standard library. For background, see https://golang.org/issue/1108.
    • This package is not implemented on Plan 9.
    • This package is not implemented on NaCl (Native Client).