I'm trying to generate the documentation for my packages in Golang. Ultimately I want to generate the documentation as part of CI/CD and host it inside our network. I know about godoc and using the instructions here I tried exactly as it states, in my package directory, however all I get is the full documentation for go itself, but not custom packages.
If I go to :6060/src, I can see a list of the contents of the src directory, and within that, I can see
However when I click on it, I get the response
I don't know if that's why I cant see my packages in the documentation, it would seem odd as $GOPATH is set to Users/me/go.
But if that's not related, how can I generate the static html files for specific packages such as my own? It seems much harder to google an answer for this that I expected. I know other tools use doxygen, but godoc doesn't seem equivalent in any way
Related
I'm writing my first Go code which among other things, sends an email. After finding out that the package net/smtp only supports Plain Auth (but some providers like outlook doesn't support it), I asked for a solution and got pointed out to https://gist.github.com/andelf/5118732.
That code works like a charm, but as it's not something written by myself, I would like to add it in a separate file and just reference it in my main.go.
What's the right approach to have multiple files in the same package? I don't want to create a different package just for that code, first because it's not mine, and secondly, because I think it's an "overkill" approach, isn't it?
I thought that as long as the files are in the same directory, I could have many *.go files, but it seems it's not working out. If I just create a file with the content of that gist, the compiler fails because expected package, found import. If I add something like package auth, then it fails because found packages auth (auth.go) and main (main.go)
So, what's the general practice in this situations? Just create packages for everything?
You can have only one package in a directory, and it looks like you don't need a package for this addition, so you can simply put that in a separate file, and add package main at the top. Having a main package and putting everything under it works up to a point. As things get larger, you have to break it up into self-contained packages.
If your current working directory is in GOPATH, then you can just add new go file with same package name main.
If your current working directory is not in GOPATH, you can still put them in multiple go files and when you run the program, you should use go run *.go instead of just go run main.go.
There are also other options which you can refer Run code with multiple files in the same main package in GoLang for detail.
I"m trying to contribute for a project and the docs tell me to use this command
go get github.com/foo/bar
but the error is
can't load package: package github.com/foo/bar: no Go files in /home/f/go/src/github.com/foo/bar
Obviously its looking on my computer but how do I make it so that it downloads from the web?
The problem is that the project you're trying to download cannot be built, because Go can't find any source files to build at the source path github.com/foo/bar. The package is, however, downloaded, and if you look in $GOPATH/src/github.com/foo/bar you will see the repository cloned there. So if you want just that, then you're done, but you can use go get -d in the future to avoid the error message.
If you want something specific that can be imported, e.g. github.com/foo/bar/somepackage, then you should use go get github.com/foo/bar/somepackage.
I'm trying to debug Go implementation of ethereum(link), because my core interest is in developing new consensus algorithm (i.e. modify the open source Go code from github).
However, I'm having problem with the location/path of the source code. When I put the folder(i.e. go-ethereum) outside of the $GOPATH and then try to compile&debug geth(go-ethereum/cmd/geth/main.go) it shows the following error: Use of internal package is not allowed.
From that error message, I figured out that the import github.com/ethereum/go-ethereum was not importing my source, and instead it was getting code from internet(like other libraries). Which of course is definitely what I shouldn't do when I'm trying to modify the github.com/ethereum/go-ethereum package code.
So, my workaround was to clone the source code into $GOPATH/src/github.com/ethereum/go-ethereum and followed this answer, and Goland IDE started compiling&debugging without error (wasn't able to go build ./cmd/geth/main.go though due to error undefined: configFileFlag...)
Thereby now I've got a working debugger that can debug with my source code modification, but this doesn't look like ideal source code structure.
The question is:
Is putting source code inside $GOPATH(because of internals) a proper approach? If so, what if I was using go-ethereum package from another project?(Fortunately I'm not, but I'm curious) Do I have to stash&revert changes I made to the code?
Yes, the folder structure you ended up with is the right one.
Code should be under $GOPATH/src as you describe.
But note that $GOPATH is not a fixed folder in your system, you can have multiple projects under different folders and change the value of $GOPATH accordingly depending what you are working on (or have multiple console terminals open, each with its own $GOPATH value).
I have a fairly large project in github that I would like not to download in its entirety. The file is mostly Go based. Unfortunately, most of the file's packages call each other. I would like to use some of the files within this repo to test my code before I push it with the rest. Is their any way to import it into golang without downloading the file (using go get github.com/foo)?
As stated above, I've tried using go get github.com/etc. but it's way to large for that.
There's no tool I know of that could do this. You could use go list -f '{{ .Imports }}' or similar to list the dependencies you need for any given Go package, and script a way to download only the files needed. But you also have to think about things like templates or config files that your programs might access too.
Alternatively, and I have no idea if this will work, you could try mounting your Git repository as a FUSE mount. I found this in a quick Google search. This will let you download the files you need on demand, and of course when you compile your program, it will only include the code needed.
I would like to use signatures for a program that I am writing in Go, but I can't figure out the documentation, which is here. In particular, I would like to use the SignPKCS1v15 and VerifyPKCS1v15 functions, but I'm not sure exactly what I have to pass as arguments. I would greatly benefit from some example code of these two functions. Thanks.
Note: The message that I would like to send is a struct that I defined.
I think the src\pkg\crypto\rsa\pkcs1v15_test.go file in the Go source tree should be a good start.
An update striving provide more context… Go source contains many tests for the code in its standard library (and the crypto/rsa package is a part of it), so whenever you have no idea how to use a standard package (or, actually, any other Go package), a good place to start is to look at the tests involving that package as testing code naturally uses the package! Tests are kept in files ending in _test.go, usually have meaningful names and are located in the same directories actual code implementing a particular package is kept.
So in your particular case you could do this:
Download the Go source package of the version matching your compiler (what go version shows) and unpack it somewhere.
Navigate to the directory matching the package of interest. Code for standard Go packages is located in the "pkg" directory under the "src" top-level directory, so if you're interested in the crypto/rsa package, you need the src/pkg/crypto/rsa directory.