Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
2023-2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mohamed.abouhoula@studenti.unimi.it
2023-2024
Commits
6ecd4501
Commit
6ecd4501
authored
Dec 14, 2023
by
Anna Morpurgo
Browse files
Options
Downloads
Patches
Plain Diff
note Scanf: minor fixes
parent
c6292821
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Lab09/noteScanf.md
+7
-6
7 additions, 6 deletions
Lab09/noteScanf.md
with
7 additions
and
6 deletions
Lab09/noteScanf.md
+
7
−
6
View file @
6ecd4501
##
# Estrazione di dati da una stringa con un formato dato
# Estrazione di dati da una stringa con un formato dato
Quando le righe o le stringhe in input hanno un
**formato fissato**
,
-
se c'è
**un solo tipo di separatore**
, si usa la funzione
-
se
nella stringa
c'è
**un solo tipo di separatore**
, si usa la funzione
`strings.Split(s, sep string)`
che restituisce la slice delle sottostringhe
-
Esempio: data nel formato
`gg/mm/aaaa`
```
go
...
...
@@ -11,12 +11,11 @@ Quando le righe o le stringhe in input hanno un **formato fissato**,
```
Il giorno, il mese e l'anno vengono salvati come elementi nella slice
-
se ci sono
**diversi tipi di separatori**
, il package
`fmt`
mette a disposizione
-
se
nella stringa
ci sono
**diversi tipi di separatori**
, il package
`fmt`
mette a disposizione
la funzione
`Scanf`
, che usa una sintassi analoga a
`Printf`
per leggere e memorizzare valori (invece di stamparli):
```
go
fmt
.
Scanf
(
format
string
,
lista
di
puntatori
a
var
)
```
```
go
var
anno
,
mese
,
giorno
,
ore
,
min
,
sec
int
...
...
@@ -25,11 +24,13 @@ la funzione `Scanf`, che usa una sintassi analoga a `Printf` per leggere e memor
//leggo data e orario (da tastiera), con i valori
//di anno, mese, giorno, ore, min, sec in una stringa in formato AAAA-MM-GG/hh:mm:ss
fmt
.
Scanf
(
"%d-%d-%d/%d:%d:%d"
,
&
anno
,
&
mese
,
&
giorno
,
&
ore
,
&
min
,
&
sec
)
```
E anche Sscanf per estrarre dati da una stringa
Il package
`fmt`
mette a disposizione anche la funzione
`Sscanf`
per estrarre dati da una stringa
```
go
fmt
.
Scanf
(
s
string
,
format
string
,
lista
di
puntatori
a
var
)
```
-
Esempio:
```
go
stringData
:=
"15/12/2022"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment