Skip to content

search

import "github.com/wnjoon/go-yfinance/pkg/search"

Package search provides Yahoo Finance search functionality.

Overview

The search package allows you to search for stock symbols, company names, and other financial assets on Yahoo Finance. It also retrieves related news articles, lists, and research reports.

Basic Usage

s, err := search.New()
if err != nil {
    log.Fatal(err)
}
defer s.Close()

result, err := s.Search("AAPL")
if err != nil {
    log.Fatal(err)
}
for _, quote := range result.Quotes {
    fmt.Printf("%s: %s (%s)\n", quote.Symbol, quote.ShortName, quote.Exchange)
}

Search Methods

The Search struct provides the following methods:

Search Parameters

Use [models.SearchParams] to customize your search:

params := models.SearchParams{
    Query:            "Apple",
    MaxResults:       10,
    NewsCount:        5,
    EnableFuzzyQuery: true,
}
result, err := s.SearchWithParams(params)

Thread Safety

All Search methods are safe for concurrent use from multiple goroutines.

Index

type Option

Option is a function that configures a Search instance.

type Option func(*Search)

func WithClient

func WithClient(c *client.Client) Option

WithClient sets a custom HTTP client.

Search provides Yahoo Finance search functionality.

type Search struct {
    // contains filtered or unexported fields
}

func New

func New(opts ...Option) (*Search, error)

New creates a new Search instance.

Example:

s, err := search.New()
if err != nil {
    log.Fatal(err)
}
defer s.Close()

func (*Search) Close

func (s *Search) Close()

Close releases resources used by the Search instance.

func (*Search) News

func (s *Search) News(query string, newsCount int) ([]models.SearchNews, error)

News returns only the news results for a search query.

Example:

news, err := s.News("Apple", 10)
for _, n := range news {
    fmt.Printf("%s: %s\n", n.Publisher, n.Title)
}

func (*Search) Quotes

func (s *Search) Quotes(query string, maxResults int) ([]models.SearchQuote, error)

Quotes returns only the quote results for a search query.

Example:

quotes, err := s.Quotes("Apple", 10)
for _, q := range quotes {
    fmt.Printf("%s: %s\n", q.Symbol, q.ShortName)
}

func (s *Search) Search(query string) (*models.SearchResult, error)

Search searches for symbols and returns matching quotes.

This is a convenience method that uses default parameters.

Example:

result, err := s.Search("AAPL")
if err != nil {
    log.Fatal(err)
}
for _, quote := range result.Quotes {
    fmt.Printf("%s: %s\n", quote.Symbol, quote.ShortName)
}

func (*Search) SearchWithParams

func (s *Search) SearchWithParams(params models.SearchParams) (*models.SearchResult, error)

SearchWithParams searches with custom parameters.

Example:

params := models.SearchParams{
    Query:            "Apple",
    MaxResults:       10,
    NewsCount:        5,
    EnableFuzzyQuery: true,
}
result, err := s.SearchWithParams(params)