Module Init code reference undefined SOLVED

Package main

package main
import ("fmt";"svr";"shutdown")

func main(){
fmt.Println("Inits ran OK");
}

src/svr/svr.go

package svr

import ("net";"os";"log";"fmt")

func init(){
Link,err:= net.Listen("unix","/tmp/XXXX");
if err!=nil{
	log.Fatal("Terminating as unable to setup Database link");
	os.Exit(1);}
go func(){
	for{
		_,err:=Link.Accept();
		if err!=nil{
			log.Fatal("Accept failed ",err);}
		fmt.Println("Accept rcvd");
		}
	}();
}

src/shutdown/shutdown.go

package shutdown

import ("svr";"net";"os";"os/signal";"syscall")

func init(){
	sigc:= make(chan os.Signal,1);
	signal.Notify(sigc,os.Interrupt,syscall.SIGTERM)
	go func(link net.Listener, c chan os.Signal){
		sig:= <-c;
		println("Shutting Down Now");
		link.Close();
		os.Exit(0);
	}(svr.Link,sigc);
	println("Shutdown handler initialised");
    }

go run test.go gives me

bjc@sol2 ~/go4 $ go run test.go

shutdown

src/shutdown/shutdown.go:13:5: undefined: svr.Link

Would anyone be good enough to tell me what I am missing ?

Thanks in advancePreformatted text

The problem is that “:=” defines a local variable so Link is not externalised.

Corrected svr.go

package svr

import ("net";"os";"log";"fmt")

var Link net.Listener;
var err error;

func init(){
Link,err= net.Listen("unix","/tmp/XXXX");
if err!=nil{
	log.Fatal("Terminating as unable to setup Database link");
	os.Exit(1);}
go func(){
	for{
		_,err:=Link.Accept();
		if err!=nil{
			log.Fatal("Accept failed ",err);}
		fmt.Println("Accept rcvd");
		}
	}();

}

Hint: Please markup code snippets using the </> button above the edit box. The code will be much more readable.

Done as requested

You can also post code like this: How to post code on this forum

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.