Convert a slice of strings into slice of float32 - go

In my program, I add a string of numbers (ex: 54 43.3 -43.2) to a slice via bufio scanner. Then I want to split the slice at each space into another slice to convert it to float32. Here is what i have:
var newSlice []float32
sliceScan = scanner.Text()
s := strings.Split(sliceScan, " ")
for i:= 0; i < len(s); i+=1 {
newSlice[i] = (float32) s[i]
}
when I run this I get this error:
syntax error: unexpected s at the end of statement

You can use strconv.ParseFloat:
var newSlice []float32
sliceScan = scanner.Text()
s := strings.Split(sliceScan, " ")
for i:= 0; i < len(s); i+=1 {
f64, err := strconv.ParseFloat(s[i], 32)
newSlice = append(newSlice, float32(f64))
}
or probably better:
sliceScan = scanner.Text()
s := strings.Split(sliceScan, " ")
newSlice := make([]float32, len(s), len(s))
for i:= 0; i < len(s); i+=1 {
f64, err := strconv.ParseFloat(s[i], 32)
newSlice[i] = float32(f64)
}

Related

Handling command drift on golang

when entering data for the array from the user side. After entering n = 5 the end a[0] is automatically assigned to 0 and is ignored to continue typing a[1]. I tried on other machines and replit but it seems to only happen on my computer. I also tried uninstalling and reinstalling golang but it didn't fix it
package main
import (
"fmt"
)
func main() {
var a = [100]int{}
var n int
fmt.Print("N = ")
fmt.Scanf("%v", &n)
for i := 0; i < n; i++ {
fmt.Printf("a[%v] = ", i)
fmt.Scanf("%v", &a[i])
}
for i := 0; i < n; i++ {
fmt.Printf("%v ", a[i])
}
fmt.Println()
}
This will solve the problem for you, it's important to debug in go language.
var a = make([]int, 100)
var n int = 5
fmt.Print("N = ")
fmt.Scanf("%v \n", &n)
for i := 0; i < n; i++ {
fmt.Printf("a[%v] = ", i)
_, err := fmt.Scanf("%v \n", &a[i])
if err != nil {
fmt.Println("error", err)
}
}
for i := 0; i < n; i++ {
fmt.Printf("%v ", a[i])
}
fmt.Println()
https://pkg.go.dev/fmt#pkg-overview

How to collect values from a channel into a slice in Go?

Suppose I have a helper function helper(n int) which returns a slice of integers of variable length. I would like to run helper(n) in parallel for various values of n and collect the output in one big slice. My first attempt at this is the following:
package main
import (
"fmt"
"golang.org/x/sync/errgroup"
)
func main() {
out := make([]int, 0)
ch := make(chan int)
go func() {
for i := range ch {
out = append(out, i)
}
}()
g := new(errgroup.Group)
for n := 2; n <= 3; n++ {
n := n
g.Go(func() error {
for _, i := range helper(n) {
ch <- i
}
return nil
})
}
if err := g.Wait(); err != nil {
panic(err)
}
close(ch)
// time.Sleep(time.Second)
fmt.Println(out) // should have the same elements as [0 1 0 1 2]
}
func helper(n int) []int {
out := make([]int, 0)
for i := 0; i < n; i++ {
out = append(out, i)
}
return out
}
However, if I run this example I do not get all 5 expected values, instead I get
[0 1 0 1]
(If I uncomment the time.Sleep I do get all five values, [0 1 2 0 1], but this is not an acceptable solution).
It seems that the problem with this is that out is being updated in a goroutine, but the main function returns before it is done updating.
One thing that would work is using a buffered channel of size 5:
func main() {
ch := make(chan int, 5)
g := new(errgroup.Group)
for n := 2; n <= 3; n++ {
n := n
g.Go(func() error {
for _, i := range helper(n) {
ch <- i
}
return nil
})
}
if err := g.Wait(); err != nil {
panic(err)
}
close(ch)
out := make([]int, 0)
for i := range ch {
out = append(out, i)
}
fmt.Println(out) // should have the same elements as [0 1 0 1 2]
}
However, although in this simplified example I know what the size of the output should be, in my actual application this is not known a priori. Essentially what I would like is an 'infinite' buffer such that sending to the channel never blocks, or a more idiomatic way to achieve the same thing; I've read https://blog.golang.org/pipelines but wasn't able to find a close match to my use case. Any ideas?
In this version of the code, the execution is blocked until ch is closed.
ch is always closed at the end of a routine that is responsible to push into ch. Because the program pushes to ch in a routine, it is not needed to use a buffered channel.
package main
import (
"fmt"
"golang.org/x/sync/errgroup"
)
func main() {
ch := make(chan int)
go func() {
g := new(errgroup.Group)
for n := 2; n <= 3; n++ {
n := n
g.Go(func() error {
for _, i := range helper(n) {
ch <- i
}
return nil
})
}
if err := g.Wait(); err != nil {
panic(err)
}
close(ch)
}()
out := make([]int, 0)
for i := range ch {
out = append(out, i)
}
fmt.Println(out) // should have the same elements as [0 1 0 1 2]
}
func helper(n int) []int {
out := make([]int, 0)
for i := 0; i < n; i++ {
out = append(out, i)
}
return out
}
Here is the fixed version of the first code, it is convoluted but demonstrates the usage of sync.WaitGroup.
package main
import (
"fmt"
"sync"
"golang.org/x/sync/errgroup"
)
func main() {
out := make([]int, 0)
ch := make(chan int)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := range ch {
out = append(out, i)
}
}()
g := new(errgroup.Group)
for n := 2; n <= 3; n++ {
n := n
g.Go(func() error {
for _, i := range helper(n) {
ch <- i
}
return nil
})
}
if err := g.Wait(); err != nil {
panic(err)
}
close(ch)
wg.Wait()
// time.Sleep(time.Second)
fmt.Println(out) // should have the same elements as [0 1 0 1 2]
}
func helper(n int) []int {
out := make([]int, 0)
for i := 0; i < n; i++ {
out = append(out, i)
}
return out
}

Goroutines: Where to close

I am struggling to understand where I should close my channels.
This bit of code takes about 0.7 seconds:
options := [3]string{"0", "1", "2"}
str := fmt.Sprintf("%6d ", id)
for j := 0; j < 40000; j++ {
str += options[rand.Intn(3)]
}
str += "\n"
Adding an io.Writestring does not make a difference to the time, so the problem is this bit.
I want roughly 100,000 records like these so I thought to put in a goroutine.
func main() {
file, _ := os.Create("myfile.txt")
ch := make(chan string)
for i := 0; i < 100000; i++ {
go generate(i, ch)
}
counter := 0
for result := range ch {
counter++
io.WriteString(file, result)
if counter == 100000 {
close(ch)
}
}
file.Close()
}
func generate(id int, c chan string) {
options := [3]string{"0", "1", "2"}
str := fmt.Sprintf("%6d ", id)
for j := 0; j < 40000; j++ {
str += options[rand.Intn(3)]
}
str += "\n"
c <- str
}
From what I understand is that I am closing the channel on the receiver side, which is not ideal? Also, this way all 100,000 should be sent to goroutines first, before I can receive any. Can I send requests to generate a record and start receiving at the same time?
Using a counter to close your channel is not a good practise.You can make use of sync.WaitGroup. This allows you to have a better control on when to close your channel:
func main() {
var wg sync.WaitGroup
ch := make(chan string)
file, _ := os.Create("myfile.txt")
for i := 0; i < 100000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
options := [3]string{"0", "1", "2"}
str := fmt.Sprintf("%6d ", i)
for j := 0; j < 40000; j++ {
str += options[rand.Intn(3)]
}
str += "\n"
ch <- str
}(i)
}
go func() {
wg.Wait()
close(ch)
}()
for result := range ch {
io.WriteString(file, result)
}
file.Close()
}
See if this solves your problem..
func main() {
file, _ := os.Create("myfile.txt")
ch := make(chan string)
wg := new(sync.WaitGroup)
for i := 0; i < 100000; i++ {
wg.Add(1)
go generate(i, ch)
}
go func(){wg.Wait();close(ch)}()
counter := 0
for result := range ch {
counter++
io.WriteString(file, result)
}
file.Close()
}
func generate(id int, c chan string) {
options := [3]string{"0", "1", "2"}
str := fmt.Sprintf("%6d ", id)
for j := 0; j < 40000; j++ {
str += options[rand.Intn(3)]
}
str += "\n"
c <- str
wg.Done()
}

cannot assign to v[i], reference to array problem?

I am trying to solve a puzzle to practice my Go. But, I am a little stuck and the error is not very helpful.
./prog.go:22:23: cannot assign to v[j]
./prog.go:22:23: cannot assign to v[wLen - 1 - j]
func SpinWords(str string) string {
ws := strings.Split(str, " ")
for i := 0; i < len(ws); i++ {
v := ws[i]
wLen := len(v)
if wLen > 4 {
for j := 0; j < wLen/2; j++ {
v[j], v[wLen-1-j] = v[wLen-1-j], v[j]
}
ws[i] = v
}
}
return strings.Join(ws, " ")
}
Almost working code here: https://play.golang.org/p/j9BYk642bFa
You can't assign to elements of v because v is a string and strings are immutable. You can convert the string to a []byte first, and then work with the elements of it, but it is not safe if your strings contain multi-byte characters.
v:=[]byte(ws[i])
Or you can convert the string to a []rune and work with it:
v:=[]rune(ws[i])
Then you can assign to elements of v, and when you're done, convert it back to a string:
str:=string(v)
If you want to performed the action then you have to convert word from string to []rune
This code works :)
package main
import (
"fmt"
"strings"
)
func main() {
result := SpinWords("Welcome to the jungle we got fun and games")
fmt.Println(result)
}
func SpinWords(str string) string {
ws := strings.Split(str, " ")
for i := 0; i < len(ws); i++ {
v := ws[i]
wLen := len(v)
if wLen > 4 {
vinrune := []rune(v)
for j := 0; j < wLen/2; j++ {
vinrune[j], vinrune[wLen-1-j] = vinrune[wLen-1-j], vinrune[j]
}
v = string(vinrune)
ws[i] = v
}
}
return strings.Join(ws, " ")
}

How to read a data file using golang?

I have a txt file :
2
Data 5 1.32
DataSecond 4 5.41
4
...
And so on. How to read the first line to know the count and then go on spliting the other lines to get individual parameters? I tried doing as follows, but it is obviously wrong.
f, err := os.Open("DATA.txt")
check(err)
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
count, err := strconv.Atoi(line)
check(err)
for i := 0; i < count; i++ {
testArray := strings.Fields(s.Text())
for _, v := range testArray {
fmt.Println(v)
}
}
}
You just forgot to Scan() inside the inner loop.
f, err := os.Open("DATA.txt")
check(err)
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
count, err := strconv.Atoi(line)
check(err)
for i := 0; i < count && s.Scan(); i++ {
testArray := strings.Fields(s.Text())
for _, v := range testArray {
fmt.Println(v)
}
}
}
You could do something like this, read the single lines into a count int
and use it as a test. If count is > 0 then use sscanf to get the other values
func main() {
f, err := os.Open("DATA.txt")
check(err)
s := bufio.NewScanner(f)
count := 0
for s.Scan() {
line := s.Text()
if count < 1 {
count, err = strconv.Atoi(line)
check(err)
continue
}
count--
var tag string
var n int
var f float64
fmt.Sscanf(line, "%s %d %f", &tag, &n, &f)
// not sure what you really wnant to do with the data!
fmt.Println(n, f, tag)
}
}

Resources