TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Sscan, Sscanf Examples (fmt)

Use the fmt.Sscan and Sscanf methods to parse in space-separated values from strings.
Sscan, Sscanf. Parsing in values from space-separated strings is a problem that has existed for many years. Even the earliest programmers may have struggled with this problem.
With Sscan and Sscanf, we can parse strings into multiple values. Sscan uses a space as a separator. And Sscanf uses a format string to guide its parsing.
Sscan example. Here we have a string that has values in it separated by spaces. We pass the correct number of arguments (as references) to fmt.Sscan. They are filled with the parsed values.

Tip: We must pass the local variables we want to store the values in as references.

Golang program that uses fmt.Sscan package main import "fmt" func main() { // Store values from Sscan in these locals. value1 := "" value2 := "" value3 := 0 // Scan the 3 strings into 3 local variables. // ... Must pass in the address of the locals. _, err := fmt.Sscan("Bird frog 100", &value1, &value2, &value3); // If the error is nil, we have successfully parsed the values. if err == nil { // Print the values. fmt.Println("VALUE1:", value1); fmt.Println("VALUE2:", value2); fmt.Println("VALUE3:", value3); } } Output VALUE1: Bird VALUE2: frog VALUE3: 100
Sscanf. With this method, we must specify a format string to guide the parsing of the values. We can use standard format codes in the format string.

Tip: Sscanf works the same way as Sscan except we must pass a format string as the second argument.

Golang program that uses fmt.Sscanf package main import "fmt" func main() { value1 := 0 value2 := "" value3 := 0 // Use format string to parse in values. // ... We parse 1 number, 1 string and 1 number. _, err := fmt.Sscanf("10 Bird 5", "%d %s %d", &value1, &value2, &value3); if err == nil { fmt.Println("VALUE1:", value1); fmt.Println("VALUE2:", value2); fmt.Println("VALUE3:", value3); } } Output VALUE1: 10 VALUE2: Bird VALUE3: 5
Sscan versus split. With split() or fields() we can separate values and parse them. This has some advantages over Sscan, particularly for oddly-formed strings.SplitFields
A review. With fmt.Sscan and Sscanf we have built-in parsing methods for strings that contain multiple values. These methods, when used correctly, can simplify programs.fmt
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf