You’re not showing the panic trace, which would tell us where the problem is. So I’m going to just wildly guess.
The
PublicKeyFile("$HOME/.ssh/key.pub")
call will always fail, because Go does no interpret the $HOME shell variable for you. The PublicKeyFile function then returns a nil on any error, essentially ignoring the error, and you’re just taking that result and putting it in the sshConfig. If you then get an ssh connection that will probably fail to authenticate. Depending on what the ssh package does it might fail with a nil pointer dereference.
So in summary, don’t “swallow” errors and return nils. Your PublicKeyFile function should probably return an (ssh.AuthMethod, error) pair and you should check the error before using the AuthMethod.
(Oh, and if you could put your code in a code block, that’d be easier to read.)