DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

[Golang] Issues When Enabling Go Modules in Old Open Source Projects

title: [Learning Notes][Golang] Problems that may be encountered when enabling Go Modules for an old open source project (e.g., go get cannot update the version)
published: false
date: 2021-04-08 00:00:00 UTC
tags: 
canonical_url: http://www.evanlin.com/linebot-go-module-v7/
---

![](http://www.evanlin.com/images/2021/go-modules.jpeg)

## Preface:

Hello everyone, the LINE Bot Go SDK is a project that has been running for over five years, and its version number has already reached v7.8.0.

And at the beginning of this month (April 2021), the LINE Bot Go SDK has a new version update, which now supports the new features provided by the platform in March, and also updates the FLEX Msg update 2 that was released last year. Everyone is welcome to use it.

This package has been updated to version v7 before supporting Modules. As a result, I stepped on a landmine as soon as I enabled it. Thanks to the Taiwanese netizen wys1203 for sending a PR to fix it. I'll also share my related experiences with you.

## TL;DR

This article will introduce the following parts:

- [How to support Go Modules for old open source projects](#legacy-support-go-modules)
- [Problems encountered](#problems)
  - [Cannot update version by “go get”](#not-update-by-go-get)
  - [pkg.go.dev version is old](#go-dev-out-of-date)
- [Go Modules support for v2 and later](#go-module-v2)
- [How to fix it? How should those who use it modify it?](#how-to-fix)
- [Conclusion](#summary)
- [Reference articles](#refer)

## How to support Go Modules for old open source projects

![](http://www.evanlin.com/images/2021/Go_SDK.png)

LINE-BOT-SDK-GO is an open source package released by LINE for the LINE Messaging API, and it supports multiple language versions (Go., PHP, Java, Python).

The version of https://github.com/line/line-bot-sdk-go has already exceeded v7, but it has been slow to support go modules. That is, there is no `go.mod` file under the project. Therefore, you need to enable Go Modules through the following methods (Enable Go Modules)

Enter fullscreen mode Exit fullscreen mode
  • go mod init
  • go mod tidy
  • go mod vendor

The PR didn't seem to have too many problems, so the new version was released. (v7.9.0)

## Problems encountered

After the version update, it didn't seem to have too many problems. But after the version update, the following two problems occurred:

### Cannot update version by “go get”

At this time, I tried to update a package that originally used https://github.com/line/line-bot-sdk-go, and the normal update process is as follows:

Enter fullscreen mode Exit fullscreen mode

go mod tidy

go: finding module for package github.com/line/line-bot-sdk-go/linebot
go: found github.com/line/line-bot-sdk-go/linebot in github.com/line/line-bot-sdk-go v7.8.0+incompatible


The problem came out. I clearly updated the version to `v7.9.0`, but I couldn't get the latest version?

So I took a new project and tried again from scratch.

1. Copy https://github.com/line/line-bot-sdk-go/tree/master/examples/echo\_bot to your go path
2. `go mod init`
3. `go mod tidy`

The result is the same:

Enter fullscreen mode Exit fullscreen mode

go mod init

go: creating new go.mod: module github.com/kkdai/echo_bot
go: to add module requirements and sums:
go mod tidy

go mod tidy

go: finding module for package github.com/line/line-bot-sdk-go/linebot
go: found github.com/line/line-bot-sdk-go/linebot in github.com/line/line-bot-sdk-go v7.8.0+incompatible


You can refer to this [issue](https://github.com/line/line-bot-sdk-go/issues/274). Whether using `go get` or `go mod tidy`, it is impossible to successfully update the version number to the latest version. This problem has troubled me for a while.

### pkg.go.dev version is old

<a id=”go-dev-out-of-date””></a>

Let's briefly explain that https://pkg.go.dev is a package documentation website for the Golang community. Developers can search for packages by keywords and view related documentation (all content is based on information from github.com).

And through https://pkg.go.dev, you can also easily check information about many project versions, such as https://pkg.go.dev/github.com/appleboy/gofight, you can see the latest version `v2` - https://pkg.go.dev/github.com/appleboy/gofight/v2

Although https://github.com/line/line-bot-sdk-go has added the `go.mod` file, it is impossible to find the https://pkg.go.dev/github.com/github.com/line/line-bot-sdk-go/v7 folder.

At this time, thanks to the Pull Request provided by Taiwanese netizens, I got some related ideas.

## Go Modules support for v2 and later

According to the official document - [Golang-Blog Publishing v2 and beyond](https://blog.golang.org/v2-go-modules), it is mentioned that if you need to release versions after v2, because they are not backward compatible. Therefore, when releasing, the official recommends two methods:

- Create a new folder `v2` and update everything to that folder. And change `go.mod` to change the version number to `v2`.
- Or directly change the `go.mod` of the current folder to change the version number to `v2`.

#### Modify go.mod file to v2 (or later)

Enter fullscreen mode Exit fullscreen mode

$ go mod edit -module github.com/YOU/YOUR_PROJECT/v2 go.mod


Here, you need to pay attention, because `go mod init` will not automatically add the relevant version (if it exceeds v2), you can only add it yourself. Therefore, you need to "manually" add the relevant version number, that is to say, if your package may have exceeded `v2` or more, but you have never enabled go modules, then you may step on this landmine.

## How to fix it? How should those who use it modify it?

### How to fix the original package to enable Go Modules

Regarding the repair method of https://github.com/line/line-bot-sdk-go, you can refer to this [pull request](https://github.com/line/line-bot-sdk-go/pull/273).

### How can the packages used be updated correctly?

If you are using the https://github.com/line/line-bot-sdk-go package, please follow the following methods to correctly obtain the latest version.

1. 

Change all import locations to `v7`

  1. AS-IS: `import "github.com/line/line-bot-sdk-go/linebot" `
  2. TO-BE: `import "github.com/line/line-bot-sdk-go/v7/linebot" `
2. 

Re-modify `go.mod`. By executing

  1. `go mod tidy`
  2. `go mod vendor`
3. 

Test your local code and make sure there are no problems.

4. 

Update to GitHub

## Conclusion:

Because Go Modules actually started to be used two years ago, 1.11, but many projects didn't actually enable it immediately. If Go Modules is not enabled, the version update will not go wrong. But once Go Modules is enabled, you have to be careful about the related cases provided in this article.

In fact, enabling Go Modules is quite convenient, and you should prepare the relevant corrections in advance. I hope that every package manager can prepare to upgrade to Go Modules package support as soon as possible.

## Related articles:

- [Go Modules: v2 and Beyond](https://blog.golang.org/v2-go-modules)
- [Module release and versioning workflow](https://golang.org/doc/modules/release-workflow)
- [How to release the v2 or higher version in Go module](https://blog.wu-boy.com/2019/06/how-to-release-the-v2-or-higher-version-in-go-module/)
- [line-bot-sdk-go #273 upgrade major version to v7 for meets the specification of go module](https://github.com/line/line-bot-sdk-go/pull/273)
- [line-bot-sdk-go #274 Bug Report - Could not update to latest version v7.9.0](https://github.com/line/line-bot-sdk-go/issues/274)
- [Golang-Blog Publishing v2 and beyond](https://blog.golang.org/v2-go-modules)
- [Go Doc: Publishing breaking API changes](https://golang.org/doc/modules/release-workflow#breaking)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)