How to get the middle character from a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"math"
)

func main() {
	s := "abcdefg"
	
	index := int(math.Floor(float64(len(s)) / 2))
	
	fmt.Println(string(s[index]))
}


/*
run:

d

*/

 



answered Sep 10, 2024 by avibootz

Related questions

1 answer 123 views
1 answer 106 views
1 answer 126 views
1 answer 110 views
1 answer 123 views
...