forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type URLParameterExtractor interface { | ||
Extract(url string) ([]string, error) | ||
} | ||
|
||
type SimpleURLParameterExtractor struct{} | ||
|
||
func NewSimpleURLParameterExtractor() *SimpleURLParameterExtractor { | ||
return &SimpleURLParameterExtractor{} | ||
} | ||
|
||
func (e *SimpleURLParameterExtractor) Extract(url string) ([]string, error) { | ||
if !strings.Contains(url, "?") { | ||
return nil, fmt.Errorf("no parameters found in URL") | ||
} | ||
|
||
parts := strings.Split(url, "?") | ||
if len(parts) < 2 { | ||
return nil, fmt.Errorf("invalid URL format") | ||
} | ||
|
||
paramsPart := parts[1] | ||
params := strings.Split(paramsPart, "&") | ||
values := []string{} | ||
|
||
for _, param := range params { | ||
pair := strings.Split(param, "=") | ||
if len(pair) == 2 { | ||
values = append(values, pair[1]) | ||
} else { | ||
return nil, fmt.Errorf("invalid parameter format") | ||
} | ||
} | ||
|
||
return values, nil | ||
} | ||
|
||
func main() { | ||
url := "https://retosdeprogramacion.com?year=2023&challenge=0" | ||
extractor := NewSimpleURLParameterExtractor() | ||
|
||
values, err := extractor.Extract(url) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
fmt.Println("Extracted values:", values) | ||
} |