Created
November 25, 2018 06:23
-
-
Save watiko/3cff2bdc325a51d1abc30d9dbb240355 to your computer and use it in GitHub Desktop.
Go without Tooling System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## セットアップ | |
```bash | |
$ mkdir bin | |
$ go run tools.go | |
``` | |
## 実行 | |
```bash | |
$ bin/gin --appPort 8080 run go run main.go | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"github.com/labstack/echo" | |
) | |
func main() { | |
e := echo.New() | |
e.GET("/", func(c echo.Context) error { | |
return c.String(http.StatusOK, "Hello, World!") | |
}) | |
e.Logger.Fatal(e.Start(":8080")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build tools | |
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
"strings" | |
_ "github.com/codegangsta/gin/lib" // これは微妙な書き方かもしれない | |
) | |
func main() { | |
install_bin("gin", "github.com/codegangsta/gin") | |
teardown() | |
} | |
func install_bin(bin_name string, package_name string) { | |
bin_location := fmt.Sprintf("bin/%s", bin_name) | |
cmd := exec.Command("go", "build", "-o", bin_location, package_name) | |
out, err := cmd.CombinedOutput() | |
fmt.Printf("cmd: %s\n", strings.Join(cmd.Args, " ")) | |
fmt.Printf("out: %s\n", out) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "err: %s\n", err) | |
} | |
} | |
func teardown() { | |
// go.mod に indirect な依存関係が追加されてしまうので綺麗にしている | |
// ref: https://github.com/golang/go/issues/26474#issuecomment-407519043 | |
_, err := exec.Command("go", "mod", "tidy").CombinedOutput() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "err: %s\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment