質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

RSS

RSS(Really Simple Syndication)はブログのエントリやニュースの見出し、標準のフォーマットの音声やビデオなどを発行するために使われるウェブフィードのフォーマットの集合体です。

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

パース

パースとは、一定の文法に従って記述されたテキスト文書を解析し、データ構造の集合体に分解・変換することを呼びます。

Q&A

解決済

2回答

856閲覧

【Go】本質的には同じ型なのに外部ライブラリでそれぞれ定義しているためエラーになる

Kota.Y

総合スコア25

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

RSS

RSS(Really Simple Syndication)はブログのエントリやニュースの見出し、標準のフォーマットの音声やビデオなどを発行するために使われるウェブフィードのフォーマットの集合体です。

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

パース

パースとは、一定の文法に従って記述されたテキスト文書を解析し、データ構造の集合体に分解・変換することを呼びます。

0グッド

1クリップ

投稿2021/03/28 06:42

複数のRSSフィードを統合して一つのRSSフィードを配信するコードをGoで書いているのですが、RSSフィードの型そのものはもちろん一つに定まるにもかかわらず、RSSフィードを取得するライブラリと新しく生成するライブラリが別の型と認識されてエラーが出ます

go

1type Link struct { 2 Href, Rel, Type, Length string 3} 4 5type Author struct { 6 Name, Email string 7} 8 9type Image struct { 10 Url, Title, Link string 11 Width, Height int 12} 13 14type Enclosure struct { 15 Url, Length, Type string 16} 17 18type Item struct { 19 Title string 20 Link *Link 21 Source *Link 22 Author *Author 23 Description string // used as description in rss, summary in atom 24 Id string // used as guid in rss, id in atom 25 Updated time.Time 26 Created time.Time 27 Enclosure *Enclosure 28 Content string 29} 30 31type Feed struct { 32 Title string 33 Link *Link 34 Description string 35 Author *Author 36 Updated time.Time 37 Created time.Time 38 Id string 39 Subtitle string 40 Items []*Item 41 Copyright string 42 Image *Image 43}

go

1type Item struct { 2 Title string `json:"title,omitempty"` 3 Description string `json:"description,omitempty"` 4 Content string `json:"content,omitempty"` 5 Link string `json:"link,omitempty"` 6 Updated string `json:"updated,omitempty"` 7 UpdatedParsed *time.Time `json:"updatedParsed,omitempty"` 8 Published string `json:"published,omitempty"` 9 PublishedParsed *time.Time `json:"publishedParsed,omitempty"` 10 Author *Person `json:"author,omitempty"` // Deprecated: Use item.Authors instead 11 Authors []*Person `json:"authors,omitempty"` 12 GUID string `json:"guid,omitempty"` 13 Image *Image `json:"image,omitempty"` 14 Categories []string `json:"categories,omitempty"` 15 Enclosures []*Enclosure `json:"enclosures,omitempty"` 16 DublinCoreExt *ext.DublinCoreExtension `json:"dcExt,omitempty"` 17 ITunesExt *ext.ITunesItemExtension `json:"itunesExt,omitempty"` 18 Extensions ext.Extensions `json:"extensions,omitempty"` 19 Custom map[string]string `json:"custom,omitempty"` 20} 21 22// Person is an individual specified in a feed 23// (e.g. an author) 24type Person struct { 25 Name string `json:"name,omitempty"` 26 Email string `json:"email,omitempty"` 27} 28 29// Image is an image that is the artwork for a given 30// feed or item. 31type Image struct { 32 URL string `json:"url,omitempty"` 33 Title string `json:"title,omitempty"` 34} 35 36// Enclosure is a file associated with a given Item. 37type Enclosure struct { 38 URL string `json:"url,omitempty"` 39 Length string `json:"length,omitempty"` 40 Type string `json:"type,omitempty"` 41}

上の二つの型定義ファイルはRSSフィードをパースするライブラリ(mmcdole/gofeed)とフィードを作成するライブラリ(gorilla/feeds)で定義されているフィードの構造体です。

取得したItemsを新たに作成するフィードのItemsとして渡したいのですが、その段階で

Cannot use 'items' (type []*"github.com/mmcdole/gofeed".Item) as type []*"github.com/gorilla/feeds".Item

というエラーが出ます。これはどう解決すれば良いのでしょうか。もし完璧に解決する方法がないのであればTypeScriptで言うanyのように型ガードを外せる方法を教えていただけると幸いです

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

本質的に同じ型であるというのならその定義を別パッケージに括り出して、
エンコード側もデコード側も同じ定義そのものを利用するように修正してみると良いと思います。

後具体的に進めるためには例えば「*Linkstringがどう相互変換される」のかを明示していく必要があると思います。

投稿2021/03/29 00:38

編集2021/03/29 00:41
nobonobo

総合スコア3367

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

TypeScript や JavaScript は動的型付け言語なので、同じ名前のプロパティがあればなんとなく動いてしまいますが、Go 言語は静的型付け言語なので、構造体のフィールドの数や順番や型がちょっとでも違えばそれらは異なる構造体であり、「anyのように型ガードを外せる方法」も存在しませんので、対応するフィールドの値をひとつひとつコピーするしかないと思います。
参考: 型の一意性 (Go プログラミング言語仕様 | gospec-ja)

投稿2021/03/28 07:22

hoshi-takanori

総合スコア7893

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問